Geo*_*ris 3 javascript authentication angularjs firebase
我正在使用Firebase作为我的主要后端解决方案,并创建了一个服务,用于在整个应用程序中处理auth(因为我开始使用simpleLogin,现在我正在尝试使用他们内置的登录方法,我认为它可能更好在从应用程序的其余部分访问它之前,只需创建所有范围).
我的firebase引用工作正常,因此它是auth方法.
但是onAuth()和offAuth()的监听方法抛出了这个错误:
Uncaught TypeError: Cannot read property 'auth' of undefined - firebase-debug.js:9954
这是firebase本身的错误还是有人看到我做错了什么?试图玩角度版本和关闭其他凉亭包如angularfire和firebase-simple-login但到目前为止没有成功.
我的auth服务代码如下所示,除了在.constant服务上定义我的url之外,我没有做任何事情.
angular.module('myApp')
.factory('auth', function (firebaseAPIUrl) {
var ref = new Firebase(firebaseAPIUrl),
onAuth = ref.onAuth,
offAuth = ref.offAuth;
onAuth(function (authData) {
console.log(authData);
});
function getCurrentUser() {
return ref.getAuth();
}
...
return {
onAuth: onAuth,
offAuth: offAuth,
getCurrentUser: getCurrentUser,
...
}
}
Run Code Online (Sandbox Code Playgroud)
在JavaScript中,当传递函数以便以后调用时,重要的是要注意调用该函数的作用域.例如,在定义闭包时,闭包将具有自己的作用域,并且this可能不再指向我们的原始对象.使用fn.bind(...)解决了这个问题.
这可能会变得棘手,因为您正在使用的代码可能取决于特定的范围.诀窍是确保范围没有改变,因为你将函数作为参数传递,这可以通过在传递方法之前将方法显式绑定到原始对象来实现.
查看http://www.reactive.io/tips/2009/04/28/binding-scope-in-javascript/和http://alistapart.com/article/getoutbindingsituations,了解有关此主题的更深入了解情况.
| 归档时间: |
|
| 查看次数: |
4645 次 |
| 最近记录: |