Shlomo
Shlomo

Reputation: 3990

ExtJs - constructor overriding my instances

I use ExtJs 4.1 and DeftJs.

Some class is defined with a constructor like that:

Ext.define( 'A.helper.Report', {
  config: {
     conf     : null,
     viewMain : null
  },
  constructor: function( oConfig ) {
     this.conf = oConfig.conf;
     this.viewMain = oConfig.viewMain;
     this.initConfig( oConfig );
  }
...

Now, I create several instances of this class like that:

var class1 = Ext.create( 'A.helper.Report', {
   conf: someValue,
   viewMain: someObject
} );

var class2 = Ext.create( 'A.helper.Report', {
   conf: otherValue,
   viewMain: otherObject
} );

When using these instances, although giving them different oConfig data, both class1 and class2 now have the data of the 2nd oConfig.

So when calling this.conf in both instances, I get someValue.

How can I keep the data of already created instances?


Solution:

I wrote

Ext.define( 'A.helper.Report', {
   ... 
  references: {}
  ...

and put my instances in there, overriding old instances.

Switched to references: null helped.

...

Upvotes: 1

Views: 2325

Answers (1)

sra
sra

Reputation: 23973

Be careful to not messing around with prototype objects...

Rewritten answer

You are doing it the wrong way.

See this working JSFiddle

Ext.define('A.helper.Report', {
     config: {
         conf     : null,
         viewMain : null
     },
     constructor: function(cfg) {
         this.initConfig(cfg);
     }
});


var class1 = Ext.create('A.helper.Report',{
    conf: 66,
    viewMain: 'Bear'
});

var class2 = Ext.create('A.helper.Report',{
    conf: 88,
    viewMain: 'Eagle'
});

class1.getConf(); // 66
class1.getViewMain(); // Bear
class2.getConf(); // 88
class2.getViewMain(); // Eagle

Upvotes: 3

Related Questions