I have a class that fetches jsonp data. I would like to simply return a value but am unable to do so. The callback does not update the class property – not sure if it is a timing or scope issue
Ext.define("App.ContentManager", {
extend: "Ext.util.Observable",
singleton: true,
data: 'empty',
getData: function() {
this.doLoad();
console.log(a.data) //not correct - shows original value
return this.data;
},
doLoad: function(url) {
var a = this;
Ext.util.JSONP.request({
url: "http://example.com/somejson",
callbackKey: "callback",
callback: function(c) {
a.data = c;
console.log(a.data) //correct json data is here
}
})
},
});
Of course is timing issue. With
doLoad()in thegetDatefunction you just start the request and you don’t wait for it to complete in order to have the data ready in the next line. You only have the data ready in thecallback: functionof thedoLoadmethod. So whatever you need to do it’s best to send the request in one function and then in thecallback:functionhave a function that will use the data returned.Update
Also I just noticed is a scope issue too. You shout pass the scope in the JSONP request with adding a
And then in the getDate you should:
this.datasinceais local variable in thedoLoadmethod.