1. #1
    Sencha User
    Join Date
    Sep 2010
    Posts
    46
    Answers
    1
    Vote Rating
    3
    gfrancis1@gmail.com is on a distinguished road

      0  

    Default Answered: scope problem with combo callback

    Answered: scope problem with combo callback


    I have a set of Combo Boxes which are dynamically loaded using the following code:

    Code:
        for(var i = 0 ; i < strata.item.length; i++) { 
            var s_id = strata.item[i].stratum_id;
            var combo = Ext.getCmp('strata'+s_id+'_combo');
            comboCallback = function(){
                alert( s_id );
            };
            combo.store.load({
                callback: comboCallback
            });
        }
    I have some more code to put in the callback but first I need to work out why it brings up '2' both times the callback is run. I'm guessing it has to do with the asynchronous loading of the store, but I'm not sure how to refer to the in-loop value of s_id (or even i) from within the callback.
    Thanks.

  2. The function comboCallback will form a closure on s_id. In other words, it will have a live reference to the variable. By the time the callback is called (asynchronously, as you say) the value of s_id will be the last value it was assigned in the loop.

    There are a number of ways to remedy this and without knowing a little more about your code it's difficult to say for sure what the best way is. Here's one way:

    Code:
    Ext.each(strata.item, function(item) {
        var s_id = item.stratum_id;
    
        var combo = Ext.getCmp('strata' + s_id + '_combo');
    
        var comboCallback = function(){
            alert( s_id );
        };
    
        combo.store.load({
            callback: comboCallback
        });
    });

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,592
    Answers
    541
    Vote Rating
    323
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    The function comboCallback will form a closure on s_id. In other words, it will have a live reference to the variable. By the time the callback is called (asynchronously, as you say) the value of s_id will be the last value it was assigned in the loop.

    There are a number of ways to remedy this and without knowing a little more about your code it's difficult to say for sure what the best way is. Here's one way:

    Code:
    Ext.each(strata.item, function(item) {
        var s_id = item.stratum_id;
    
        var combo = Ext.getCmp('strata' + s_id + '_combo');
    
        var comboCallback = function(){
            alert( s_id );
        };
    
        combo.store.load({
            callback: comboCallback
        });
    });

Thread Participants: 1

Tags for this Thread