PDA

View Full Version : Ext.Ajax.request variable scope question



MrBeef
5 May 2012, 11:42 AM
Hi everyone,

I'm using an Ext.Ajax.request to dynamically load the 'selectfield' options on an Ext.form.Panel.

I realized that declaring variables inside of the AJAX request would not work due to the variable scope, so I declared them outside. However, when I populate my form, I still see that the field's text and value are 'undefined'. I'd appreciate any advice, thanks!



var candidate;
var opponent;

Ext.Ajax.request({
url: 'get_info.php',
success: function(response){
data = Ext.JSON.decode(response.responseText)
candidate = data.candidate;
opponent = data.opponent;
}
});

Ext.getCmp('fieldset').add([
{
xtype: 'selectfield',
name : 'c_choice',
id : 'c_choice',
label: 'Choice?',
options: [
{text: candidate, value: candidate},
{text: opponent, value: opponent}
]
}
]);

MrBeef
5 May 2012, 2:58 PM
I worked around the problem by putting my "add" function inside of the AJAX call...but I'd still be interested in learning why the first way didn't work.

evant
5 May 2012, 11:11 PM
The 'A' in Ajax stands for asynchronous. By the time your success function has run, your add call was already made:



Ext.Ajax.request({
url: 'get_info.php',
success: function(response){
console.log(new Date());
}
});
console.log(new Date());

MrBeef
6 May 2012, 6:11 AM
Thanks, that helps!