PDA

View Full Version : Extending Ext.data.Store



Alexander Bauer
5 Sep 2012, 4:37 AM
Hi there,
I am trying to extend the Ext.data.Store class, since I have many stores which follow the same properties/proxies. Therefore I declared a class


Ext.define('AbstractStore', {
extend : 'Ext.data.Store',
model : null,
....
constructor : function (config) {
var me = this;

// this makes sense
me.proxy.url = arguments[0].url || me.url;

// why I have to call this ?
if (me.model) {
me.proxy.model = me.model;
}

me.callParent([config]);
}
});


This works fine, but when having 2+ classes which extend the AbstractStore, the second class has the same model of the first one. That said:


Ext.define('StoreA', {
extend : 'AbstractStore',
model : 'modelA'
});




Ext.define('StoreB', {
extend : 'AbstractStore',
model : 'modelB'
});


StoreB will load the data all fine, but the associated model will be 'modelA' if I remove the 'me.proxy.model' part. I noticed that the Store class is being extended using prototype and all of the properties are stored on prototype. This is maybe the reason why they share properties.
Do they share the proxy instance ?

If a remove the Abstract layer, with the same config everything works fine.

tvanzoelen
5 Sep 2012, 4:47 AM
Had some kind of issue before. Just create a new instance of the model in the constructor.

If a class is extended its object properties are put on the prototype. So you have to assign a new instance of that property in the constructor.

See: http://www.sencha.com/forum/showthread.php?152606-Class-properties-are-in-someway-global

Alexander Bauer
5 Sep 2012, 4:55 AM
So basically this should do the trick?


constructor : function (config) {
var me = this;


me.proxy.url = arguments[0].url || me.url;
me.proxy = Ext.create('myProxyClass', me.proxy);
me.callParent([config]);
},

tvanzoelen
5 Sep 2012, 4:57 AM
Yup

Alexander Bauer
5 Sep 2012, 4:58 AM
Ok, thank you so much, I spent 8+ hours investigating all the src code :/
Now I always get separate instance of the proxy, which is the DESIRED behavior. :)

Alexander Bauer
5 Sep 2012, 5:24 AM
for those who have same issues, I managed it using:


// clone proxy, so its not being shared
me.proxy = Ext.Object.merge({}, me.proxy);


which is basically the same as creating a new instance, but I am more comfortable with this.

so I am having the following:


constructor : function () {
var me = this;

me.proxy.url = arguments[0].url || me.url;

// clone proxy, so its not being shared
me.proxy = Ext.Object.merge({}, me.proxy);


me.callParent(arguments);
}