2012-09-17 63 views
1

如何在調用我的回調函數時保留thisExtJs 4.1再次調用範圍

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

使用...

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

...而不是對象的回調函數的上下文特異性結合。否則,該函數將在全局上下文中調用,其功能this將參考window

+1

感謝幫助。將來會使用。 – Patrick