ExtJs 4.1再次回调范围

时间:2012-09-17 13:24:38

标签: extjs callback scope extjs4.1

如何在调用我的回调函数时保留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...

}

1 个答案:

答案 0 :(得分:5)

使用......

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

...而是专门将对象的上下文绑定到回调函数。否则,此函数将在全局上下文中调用,其this函数将引用window