Results 1 to 2 of 2

Thread: Classsystem config with array -> 2 instances have the same array-object

  1. #1
    Sencha User
    Join Date
    Mar 2012
    Posts
    137
    Answers
    8

    Default Answered: Classsystem config with array -> 2 instances have the same array-object

    Pretty simple example:

    I created a class with an array as config object (empty by default). Afterwards i instantiate 2 objects of this class with Ext.create.
    If i add an item to the array-object of 1 of the instances the array of the second one is also altered. This means that the array of both instances seems to be the same object.

    Working example: http://jsfiddle.net/SmQJv/1/
    PHP Code:
    Ext.define('Class', {
        
    config: {
            
    arr: []
        },
        
        
    constructor: function(config) {
            
    this.initConfig(config);
            
            return 
    this;
        }
    });

    Ext.onReady(function() {
        var 
    instance1 Ext.create('Class'),
            
    instance2 Ext.create('Class'),
            
    ta Ext.get('textarea');
            
        
    // output values before
        
    ta.dom.value 'before:';
        
    ta.dom.value += '\r\ninstance1 arr: ' instance1.arr;
        
    ta.dom.value += '\r\ninstance2 arr: ' instance2.arr;
        
        
    // add new item to array of instance1 and change it's index
        
    ta.dom.value += '\r\n\r\nAdd item to array of instance1 only...';
        
    instance1.arr.push(3);

        
    // output values after
        
    ta.dom.value += '\r\n\r\nafter:';
        
    ta.dom.value += '\r\ninstance1 arr: ' instance1.arr;
        
    ta.dom.value += '\r\ninstance2 arr: ' instance2.arr;
        
        
    ta.dom.value += '\r\n\r\nWhy do both instances have the same array-object?';
    }); 
    If i pass an config-object to the create-method (e.g. instance2 = Ext.create('Class', { arr: [] })) everything seems to be fine and both instances have a different array-object.

    I would really appreciate if anyone could tell me what is happening here and how i could prevent it.

    Thanks

    -----------------------------------------------------------------------------------
    edit: Simplified example a little bit..

  2. Use the built-in setter/getter that is created when using the config object:

    instance1.setArr([3]);
    after:
    instance1 arr: 3
    instance2 arr:
    To clarify, your approach is setting it at the base level.

    Scott.

  3. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,410
    Answers
    716

    Default

    Use the built-in setter/getter that is created when using the config object:

    instance1.setArr([3]);
    after:
    instance1 arr: 3
    instance2 arr:
    To clarify, your approach is setting it at the base level.

    Scott.
    Last edited by scottmartin; 24 Oct 2012 at 7:30 AM. Reason: update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •