1. #1
    Sencha User
    Join Date
    Mar 2007
    Location
    Toronto, ON, CA
    Posts
    202
    Vote Rating
    0
    timb is on a distinguished road

      0  

    Default [2.2] Ext.util.MixedCollection.clone copies reference, not value

    [2.2] Ext.util.MixedCollection.clone copies reference, not value


    Hello,

    When using the clone method of a MixedCollection, values changed in one clone are seen by other clones. This does not appear to be a true clone; it appears that it is copying the reference and not the value. Here is code that should reproduce the problem:
    Code:
    var mc = new Ext.util.MixedCollection();
    mc.addAll([
        {'id': '1', 'value': 'one'},
        {'id': '2', 'value': 'two'},
        {'id': '3', 'value': 'three'}
    ]);
    
    var mcClone1 = mc.clone();
    var mcClone2 = mc.clone();
    
    mcClone1.get('1').value = 'four';
    Ext.Msg.alert('Value should be one', mcClone2.get(1).value);
    You will see that the value of 'four' is applied to both clones for the item with key '1', not just the first clone, as I would have expected.

    I am using the final 2.2 build of Ext, ext-base, FF 3.0.3 and Windows XP SP3.

    Thanks,
    Tim

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    82
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    This is by design.

    Ext.util.MixedCollection.clone() only creates a copy of the collection itself, not of the objects in it (that would be called deep cloning).

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Location
    Toronto, ON, CA
    Posts
    202
    Vote Rating
    0
    timb is on a distinguished road

      0  

    Default


    I'm a little confused. If it's not a deep clone then what's the difference between this
    Code:
    mcClone1 = mc.clone();
    and this
    Code:
    mcClone1 = mc;
    ?

  4. #4
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by timb View Post
    I'm a little confused. If it's not a deep clone then what's the difference between this
    Code:
    mcClone1 = mc.clone();
    and this
    Code:
    mcClone1 = mc;
    ?
    so you can manipulate the collection without affecting the original.
    like adding apples / removing oranges from a basket.

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Location
    Toronto, ON, CA
    Posts
    202
    Vote Rating
    0
    timb is on a distinguished road

      0  

    Default


    OK, got it. Thanks for the clarification.

    Is it possible to expand the documentation to avoid future confusion? The current documentation is
    clone() : MixedCollection
    Creates a duplicate of this collection
    ...
    Just having the word "clone" is a little ambiguous. It would be nice if it specified that it is a shallow clone, as the description doesn't help clarify if it's a shallow clone or deep clone.

  6. #6
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by timb View Post
    OK, got it. Thanks for the clarification.

    Is it possible to expand the documentation to avoid future confusion? The current documentation is

    Just having the word "clone" is a little ambiguous. It would be nice if it specified that it is a shallow clone, as the description doesn't help clarify if it's a shallow clone or deep clone.
    i've copied your post to the doc bugs thread for consideration:
    http://extjs.com/forum/showthread.ph...528#post250528

Thread Participants: 2