【问题标题】:ExtJs 4.1 Callback scope againExtJs 4.1 再次回调作用域
【发布时间】:2012-09-09 17:22:26
【问题描述】:

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

}

【问题讨论】:

    标签: extjs callback scope extjs4.1


    【解决方案1】:

    使用...

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

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

    【讨论】:

    • 感谢您的帮助。以后会用到。
    猜你喜欢
    • 2016-04-03
    • 2013-07-24
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    相关资源
    最近更新 更多