Shlomo
Shlomo

Reputation: 3990

ExtJs 4.1 Callback scope again

How can I keep this when calling my callback function?

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...

}

Upvotes: 1

Views: 2525

Answers (1)

raina77ow
raina77ow

Reputation: 106385

Use ...

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

... instead to bind the object's context to the callback function specifically. Otherwise this function will be called in the global context, and its this function will refer to window.

Upvotes: 5

Related Questions