Hybrid View

  1. #1
    Sencha Premium Member
    Join Date
    May 2007
    Posts
    3
    Vote Rating
    0
    mqaa is on a distinguished road

      0  

    Default Problem with Array.indexOf / Array.remove

    Problem with Array.indexOf / Array.remove


    Hi there,

    I just started to us Ext and like it alot!

    But there is an issue with the extensions it add to the array object using:

    Ext.applyIf(Array.prototype, {
    indexOf : function(o){
    for (var i = 0, len = this.length; i < len; i++){
    if(this[i] == o) return i;
    }
    return -1;
    },

    remove : function(o){
    var index = this.indexOf(o);
    if(index != -1){
    this.splice(index, 1);
    }
    }
    });

    We are using the following style to go through the items in an array:

    arrayX = [];
    arrayX['key1'] = 'foo';
    arrayX['key2'] = 'bar';

    for(var key in arrayX) {
    var element = arrayX[key];
    }

    But with the addition of indexOf and remove this no longer works because these functions are now also retruned as key's in the array.

    Is there a way to avoid this problem?

    Marc

  2. #2
    Ext User
    Join Date
    Mar 2007
    Posts
    39
    Vote Rating
    0
    stekolla is on a distinguished road

      0  

    Default


    The easy solution to this is not to use an array unless you're using numeric indices. Replace the array literal with an object literal and what you're trying to do should work fine.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    51
    Vote Rating
    0
    sdrew is on a distinguished road

      0  

    Default


    I just ran into this BUG also, adding Ext to an existing application broke existing javascript functions as Ext mucked with the javascript Array prototype.

    This is a big problem that I'm sure many will run into and can't expect everyone to change their existing javascript functions. There must be a better solution?

    Steve.

  4. #4
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    The bug isn't in Ext, it's in the misuse of the Array class. If you look at the functions on the Array object they all work with indexes. Arrays themselves do not support string keys. When you add string key'ed properties, do you notice the length doesn't change? You can't slice or pop or splice them?

    However, every object in JavaScript supports dynamic expando properties. When you make a call like this:

    arrayX['key1'] = 'foo';

    you are not doing anything with the Array class, which is why the Array functions don't work on that value. You are simply assigning an expando to that JavaScript object. In fact, arrayX.key1 = 'foo'; does exactly the same thing.

    If all you want is a JavaScript object to add expandos to, the syntax is almost identical:

    arrayX = {};
    arrayX['key1'] = 'foo';
    arrayX['key2'] = 'bar';

    Lastly, why is this in BUGS?? If you want to discuss something that Ext is doing that you don't agree with, put it in the general discussion forum. That doesn't mean it's a bug.
    Jack Slocum
    Ext JS Founder
    Original author of Ext JS 1, 2 & 3.
    Twitter: @jackslocum
    jack@extjs.com

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    You'll find numerous discussions of this problem via Google.

  6. #6
    Ext JS Premium Member NoahK17's Avatar
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    518
    Vote Rating
    1
    NoahK17 is on a distinguished road

      0  

    Default


    I've had the same issue... and I was told quite forcefully by the developers that the method as coded is not going to change, unless you over-ride it yourself.

    Which is what I did.
    Noah
    Senior Web Developer
    NBA.com