Looks like we cannot reproduce this. Please provide another test case to reproduce this issue.
  1. #1
    Sencha User
    Join Date
    Feb 2011
    Posts
    111
    Vote Rating
    0
    sissonb is on a distinguished road

      0  

    Default Ext.each() params are backwards

    Ext.each() params are backwards


    The iterate and each functions have the parameters backwards.

    Ext.each(array, function(value, key)){});
    Ext.each(array, function(key,value)){});

    Ext.iterate(object, function(value, key)){});
    Ext.iterate(object, function(key,value)){});

    This applies to all versions of Touch and ExtJS.

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,408
    Vote Rating
    851
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    With this test case:

    Code:
    var arr = [
        'foo',
        'bar'
    ];
    
    Ext.each(arr, function(value, index) {
        console.log(value, index);
    });
    
    var obj = {
        foo : 'bar',
        bar : 'meh'
    };
    
    Ext.iterate(obj, function(key, value) {
        console.log(key, value);
    });
    I do not see the issue. I get this in my console:

    foo 0
    bar 1
    foo bar
    bar meh

    which to me is 100% correct according to the documentation.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    72
    Vote Rating
    13
    eric.cook will become famous soon enough

      0  

    Default Ext.Array.each vs Ext.Object.each

    Ext.Array.each vs Ext.Object.each


    I think the confusion here is caused by the function arguments for Ext.Array.each and Ext.Object.each not being consistent with each other. Functionally they work exactly as they are supposed to, but you can't use the same method for both and have it work correctly.

    Code:
    Ext.Array.each(anArray,function(arg1 /* value */, arg2 /* index */, array){
        // array[arg2] === arg1
    });
    
    Ext.Object.each(anObject, function(arg1 /* key */, arg2 /* value */, obj){
        // obj[arg1] === arg2
    });
    It would make more sense if the first two arguments for Ext.Object.each were changed to (value, key) but it's a moot point anyway since it's not going to change.

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,408
    Vote Rating
    851
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    I can understand the argument confusion but the difference is a logical one between an array and object IMO.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    72
    Vote Rating
    13
    eric.cook will become famous soon enough

      0  

    Default


    There is a logical difference between arrays and objects, true. But consider the following:

    Code:
    var myArray = [1, 2, 3];
    var myObject = {
        "0": 1,
        "1": 2,
        "2": 3,
        length: 3
    };
    In the current model, I can't iterate over these objects with the same method. Although I could do some cool JavaScript magic and come up with something like this:

    Code:
    MyApp.makeIterator = function(fn){
        return function(value, index, obj){
            if(Ext.isArray(obj)) fn.call(this, value, index, obj);
            else fn.call(this, index, value, obj);
        };
    };
    This may not be completely correct but the point is that I can reuse my iterator now without having to worry about what kind of object I'm working on.

    Just an idea to play with.

  6. #6
    Sencha User
    Join Date
    Feb 2011
    Posts
    111
    Vote Rating
    0
    sissonb is on a distinguished road

      0  

    Default


    Good ideas Eric. Maybe a function like, MyApp.makeIterator, could make its way into Sencha and the "Ext.Array.each" and "Ext.Object.each" could be depreciated.

    Check out how jQuery implemented this function http://api.jquery.com/jQuery.each/

Thread Participants: 2