Javascript Ember.js-ApplicationRoute中的this.set()不工作

Javascript Ember.js-ApplicationRoute中的this.set()不工作,javascript,ember.js,firebase,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);

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

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

  init: function() {
      if (loggedIn) {
        this.set('isLoggedIn', true);
      } else {
        console.log(error)
        this.set('isLoggedIn', false);
      }
    });
  }
});
但是,在
this.set()
上,我得到:

未捕获类型错误:未定义不是函数

有什么想法吗

我认为处理用户会话的最佳位置是
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);
      }
    });
  }
})
因此,我之前省略了我的Firebase代码,因为我并不认为它与此相关,但为了追踪问题,我将添加它。

第一件事的第一件事:)

如会议所述:

注意:如果您确实为诸如Ember.View或Ember.ArrayController之类的框架类重写init,请确保在init声明中调用它。\u super()!如果您不这样做,Ember可能没有机会进行重要的设置工作,并且您将在应用程序中看到奇怪的行为

其次,您的
loggedIn
变量既没有声明也没有实例化,因此为空,您的
error
也为空,但我猜您只是删除了项目中的一些代码来拼凑一个快速示例,因此如果您这样做:

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

  init: function() {
      this._super(); // <-- call `this._super()` here

      if (loggedIn) {
        this.set('isLoggedIn', true);
      } else {
        console.log(error)
        this.set('isLoggedIn', false);
      }
    });
  }
});
App.ApplicationRoute=Ember.Route.extend({
伊斯洛格丁:错,
init:function(){
这是第一件事:)

如会议所述:

注意:如果您确实为诸如Ember.View或Ember.ArrayController之类的框架类重写了init,请务必调用它。_super()在您的init声明中!如果您不这样做,Ember可能没有机会进行重要的安装工作,并且您会在应用程序中看到奇怪的行为

其次,您的
loggedIn
变量既没有声明也没有实例化,因此为空,您的
error
也为空,但我猜您只是删除了项目中的一些代码来拼凑一个快速示例,因此如果您这样做:

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

  init: function() {
      this._super(); // <-- call `this._super()` here

      if (loggedIn) {
        this.set('isLoggedIn', true);
      } else {
        console.log(error)
        this.set('isLoggedIn', false);
      }
    });
  }
});
App.ApplicationRoute=Ember.Route.extend({
伊斯洛格丁:错,
init:function(){

这个。_super();//原因与

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

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

原因与环境有关

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

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

首先,谢谢你的回答!我忘记了这个问题。_super()
。其次,我用完整的代码更新了问题。我正在使用Firebase,只是删除了它,因为我认为它不相关(猜得对!)但是我也可以添加它。我尝试了上面的方法,但仍然不起作用。它将注销
user
,但在
this.set()
上返回错误。刚刚看到它,但使用更新的代码,一切都是有意义的:)-这是一个范围问题:)因此,首先,感谢您的回答!我忘记了
这个问题。_super()
。其次,我用完整的代码更新了问题。我正在使用Firebase并删除了它,因为我认为它不相关(很好的猜测!),但我也可以添加它。我尝试了上面的方法,但仍然不起作用。它将注销
用户
,但在
this.set()上返回错误
刚刚看到,但通过更新代码,这一切都是有意义的:)-这是一个范围问题:)不是真的:)。当涉及到内部函数“this value”的设置时,JS语言的设计被搞砸了。我只是在过去遇到了太多的麻烦,所以当我发现类似“unde”这样的错误消息时,我首先要检查的就是它fined不是函数或其他函数..好调用。我把它放在我的工具带中。不是真的:)。当谈到内部函数的设置方式时,JS语言的设计被搞砸了。我过去对它有太多的麻烦,所以当我发现像“未定义”这样的错误消息时,我首先要检查一下“函数”之类的…好主意。我把它放在我的工具带上了。