PDA

View Full Version : [SOLVED] Ext.Ajax.request + function + scope



saprot
24 Jan 2011, 2:25 AM
Hi! How can I make someFunction to return contents of success function? I obviously can't use some temp variable because of asynchronous nature of Ajax call, so, how can I do that? Adding scope : this, doesn't help :)



function someFunction() {
Ext.Ajax.request({
url : 'some/url',
success : function(response, opts) {
return response.responseText;
}
});
}

grgur
24 Jan 2011, 2:41 AM
Why don't you try registering an event and add a listener to the someFunction caller?

No one can explain it better than Saki:
http://blog.extjs.eu/know-how/events-explained

fay
24 Jan 2011, 2:54 AM
The answer's in your question - "...asynchronous nature..." Here a good explanation by Animal as to why you need to change your approach: http://www.sencha.com/forum/showthread.php?103826-Writing-a-function-for-Ext.Ajax.request

saprot
24 Jan 2011, 7:21 AM
I'd read that thread before, but thanks :) I've partially solved my problem by using createInterceptor, but another question appeared:



if(!Ext.ComponentMgr.isRegistered(xtype)) {
Ext.Ajax.request({
url : 'some/url/returning/component',
params : {
'xtype' : xtype
},
success : function(response, opts) {
eval(Ext.decode(response.responseText).data.js); //LINE 1

console.log(Ext.ComponentMgr.isRegistered(xtype)); //returns TRUE
}
});
}

console.log(Ext.ComponentMgr.isRegistered(xtype)); //returns FALSE


Why the first console.log line returns true, and the second false? Is Ext.reg registering components in local scope? The line LINE 1 evals returned object definition (Ext.extend, Ext.reg, initComponent, itp) and if I create an object with returned xtype line below LINE 1, everything is OK. But after second console.log xtype is not found...

fay
24 Jan 2011, 7:46 AM
Again, it's because it's asynchronous! The line that returns FALSE is executed before LINE1 - LINE1 will only be executed after the Ajax request returns, while anything that follows the request call is immediately executed.

saprot
24 Jan 2011, 7:48 AM
my bad, thanks a lot :)