PDA

View Full Version : Call a function from a callback (scope problem ?)



siebmanb
30 Aug 2011, 10:02 AM
Hello everyone,
This is my first post, I hope I am posting in the right place.
I placed [...] to hide useless code and private data.

I have this sample code :

Ext.regController('Page', {
[...]
getData: function(){
Ext.util.JSONP.request({
url: 'http://[...]/index.php',
callbackKey: 'callback',
params: {
action: 'getInfo',
},
callback: function(data) {
this.refresh(data);
},
});
},

refresh: function(data)
{
alert(data);
},
});


I would like to call the "refresh" function from the callback of the JSONP request in the "getData" function. In other words, I would like the data to pop up when I call getData.
Is that even possible ?

PS : my actual problem is interacting with the View attached to the Controller. It is possible from "refresh" for example but not from the callback function.

Thanks a lot

mankz
30 Aug 2011, 12:42 PM
When passing a callback method which uses the 'this' object, you also need to tell the receiver of the callback what 'this' is supposed to mean. 'this' is not tied to the function in any way. In Ext JS you normally do this by defining the 'scope'.



Ext.util.JSONP.request({
url: 'http://[...]/index.php',
callbackKey: 'callback',
params: {
action: 'getInfo',
},
callback: function(data) {
this.refresh(data);
},
scope : this
});

siebmanb
30 Aug 2011, 12:55 PM
Thank you so much !