1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    26
    Vote Rating
    1
    MrBeef is on a distinguished road

      0  

    Default Ext.Ajax.request variable scope question

    Ext.Ajax.request variable scope question


    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!

    Code:
    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}
         ]
      }
    ]);

  2. #2
    Sencha User
    Join Date
    Feb 2012
    Posts
    26
    Vote Rating
    1
    MrBeef is on a distinguished road

      0  

    Default


    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.

  3. #3
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,912
    Vote Rating
    628
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


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

    Code:
    Ext.Ajax.request({
        url: 'get_info.php',
        success: function(response){
            console.log(new Date());
        }
    });
    console.log(new Date());
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    26
    Vote Rating
    1
    MrBeef is on a distinguished road

      0  

    Default


    Thanks, that helps!

Thread Participants: 1