2013-03-14 3 views
0

ember.jsember-auth gem을 사용하는 다음 컨트롤러가 있습니다. 이 컨트롤러는 작동하지만 난에 서명을받을 때마다 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"); 
    }); 
    } 
}); 

물론이 같은 Auth.on에 의해 호출되는 함수 내에서 trueloginError를 설정하고 싶으면 무엇을 :.

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"); 
    }); 
    } 
}); 

그러나 콜백 내부의 범위가 다르기 때문에 이것은 분명히 작동하지 않습니다. 어쩌면 나는 아주 기본적인 것을 놓치고있을거야. 어떻게 작동시킬 수 있습니까?

답변

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"); 
}); 
또한 사용할 수 있습니다
+1

[.bind (이)] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), (IE에 대한 IE9 필요) – harianus