0

我有以下控制器使用ember.jsember-authgem。该控制器有效,但loginError每次我登录时都会设置属性。

BaseApp.SignInController = Auth.SignInController.extend({
  email: null,
  password: null,
  loginError: false,
  signIn: function() {
    this.registerRedirect();
    Auth.signIn({
      email: this.get('email'),
      password: this.get('password')
    });
    this.set('loginError', true); // Sets correctly but each time
    Auth.on('signInError', function() {
      console.log("This is a signin error");
    });
  }
});

显然我想做的是设置loginErrortrue这样调用的函数内部Auth.on

BaseApp.SignInController = Auth.SignInController.extend({
  email: null,
  password: null,
  loginError: false,
  signIn: function() {
    this.registerRedirect();
    Auth.signIn({
      email: this.get('email'),
      password: this.get('password')
    });
    Auth.on('signInError', function() {
      this.set('loginError', true); // Doesn't set the controller's property
      console.log("This is a signin error");
    });
  }
});

但这显然不起作用,因为回调内部的范围不同。也许我错过了一些非常基本的东西。我怎样才能让它工作?

4

1 回答 1

3

this传递给方法的匿名函数中的上下文(即)on与控制器中的上下文不同。您可以通过将上下文保存到闭包中的不同变量来解决此问题。

var self = this;
Auth.on('signInError', function() {
  self.set('loginError', true); // Should now set the controller's property
  console.log("This is a signin error");
});
于 2013-03-14T21:28:16.740 回答