2012-09-17 2 views
1

내 콜백 함수를 호출 할 때 this을 어떻게 유지하나요?ExtJs 4.1 다시 콜백 범위

success: Ext.bind(this.readConfigCallback, this) 

init: function() { 

console.log(this); // constructor, YES! 

this.readConfig(); 
}, 

readConfig: function() { 

console.log(this); // constructor, YES! 

this.httpApi.request({ 
    module : 'monitor', 
    func  : 'getConfig', 
    success : this.readConfigCallback, 
    scope : this 
}); 
}, 

readConfigCallback: function(oResponse) { 

console.log(this); // Window, NO! 

var oView = this.getView(); // error... 

}

답변

5

사용 ... ... 대신 특별히 콜백 함수 객체의 컨텍스트를 바인딩. 그렇지 않으면이 함수는 전역 컨텍스트에서 호출되며 this 함수는 window을 참조합니다.

+1

감사합니다. 앞으로 사용할 것입니다. – Patrick