Reputation: 3990
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
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