PDA

View Full Version : Making vars acceessible outside of Ext.Ajax.request



plonk
1 Feb 2008, 4:49 AM
Hi all
I am new to ExtJS and very much struggling with some fundamental issues. This one in particular:
I have an ajax request where I get a json string. Decoding the string and assigning it to a var inside Ext.Ajax.request is now problem, But how do I make the assigned variable public?
Here is the Code


Ext.Ajax.request({
url : 'myscript.php' ,
method: 'GET',
success: function ( result, request ) {

myData = Ext.util.JSON.decode(result.responseText);
Ext.Msg.alert(myData['titel'], myData['text']); // Works

},
failure: function ( result, request) {
Ext.MessageBox.alert('Failed', result.responseText);
}
});

Ext.Msg.alert(myData['titel'], myData['text']); // Does not Work (displays empty message box.)

How do I get the alert outside Ext.Ajax.request to display anything?
Any help on this is very much appreciated!

Cheers
plonk

para
1 Feb 2008, 5:20 AM
var myData;

Ext.Ajax.request({
url : 'myscript.php' ,
method: 'GET',
success: function ( result, request ) {

window.myData = Ext.util.JSON.decode(result.responseText);
Ext.Msg.alert(myData['titel'], myData['text']); // Works

},
failure: function ( result, request) {
Ext.MessageBox.alert('Failed', result.responseText);
}
});

Ext.Msg.alert(myData['titel'], myData['text']); // Does not Work (displays empty message box.)

Condor
1 Feb 2008, 5:24 AM
That solves the scoping issue, but you forget that AJAX requests are asynchronous.

Ext.Ajax.request only sends a request to the server. The response won't be received yet when you use the Ext.Msg.alert. You can only be sure that the data is ready in the success handler.

plonk
1 Feb 2008, 6:08 AM
Thank you, this helped: I was completely lost in the public/private var issue, it never occurred to me that asynchronity was the issue.

So whatever has to be done with a response value has to be dealt with in the success handler.