Ember.js - ApplicationRoute中的this.set()不起作用

JDi*_*522 1 javascript ember.js firebase

我试图在用户登录时设置一个布尔变量.

App.ApplicationRoute = Ember.Route.extend({
  isLoggedIn: false,

  init: function() {
      if (loggedIn) {
        this.set('isLoggedIn', true);
      } else {
        console.log(error)
        this.set('isLoggedIn', false);
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

但是,this.set()我得到:

未捕获的TypeError:undefined不是函数

有任何想法吗?

我认为处理用户会话的最佳位置是App.ApplicationRoute因为它是一切的根源.这会导致问题吗?

更新:这里有反馈是当前/完整代码.

App.ApplicationRoute = Ember.Route.extend({
  isLoggedIn: false,

  init: function() {
    this._super();

    auth = new FirebaseSimpleLogin(appRef, function(error, user) {
      if (user) {
        console.log(user)
        this.set('isLoggedIn', true);
      } else {
        console.log(error)
        this.set('isLoggedIn', false);
      }
    });
  }
})
Run Code Online (Sandbox Code Playgroud)

所以我之前遗漏了我的Firebase代码,因为我并不认为它是相关的,但为了追踪问题,我会将其添加进去.

Hri*_*shi 5

原因与此有关

this
Run Code Online (Sandbox Code Playgroud)

在firebasesimplelogin中错误地设置.您在构造函数中使用的函数必须在对外部上下文的"this"的引用中传递.最简单的方法是将函数的代码更改为:

function(error, user) {
  if (user) {
    console.log(user)
    this.set('isLoggedIn', true);
  } else {
    console.log(error)
    this.set('isLoggedIn', false);
  }
}.bind(this));
Run Code Online (Sandbox Code Playgroud)