PDA

View Full Version : Wrong reference when creating objects from extended class



Oliver Specht
12 Feb 2011, 8:09 AM
Hi,
I have tried to extend the GroupingStore with an extra function by doing this:


Om.data.OrderDataStore = Ext.extend(Ext.data.GroupingStore, {
id : 'OrderDataStore',
setFilterParameter : function (parameter) {
this.baseParams['filter'] = parameter;
}
...

Then I try to create two instances of this derived class:


var ordersWithBA = new Om.data.OrderDataStore();
ordersWithBA.setFilterParameter("ORDER_BA_OPEN");

var ordersWithBADelivered = new Om.data.OrderDataStore();
ordersWithBADelivered.setFilterParameter("ORDER_BA");


which results in overwriting the first variables filter parameter (ORDER_BA_OPEN) with the second one (ORDER_BA).

Why does this happen? Don't I have two different variables?!?

Thanks for any help,
Oliver

Condor
12 Feb 2011, 8:37 AM
1. You can't specify an id in the prototype if you are planning to create multiple instances (all instances would have the same id, which isn't allowed).
2. This shouldn't happen. Are you sure you didn't also specify baseParams in the prototype (you didn't include your full extended class).

Oliver Specht
12 Feb 2011, 8:47 AM
Hi,
thanks for the quick reply!

1. I have removed the id from the inheritance code and put it into the constructor as parameter but this doesn't work either:


var ordersWithBA = new Om.data.OrderDataStore({ id : 'OrderDataStoreWithBA'});
ordersWithBA.setFilterParameter("ORDER_BA_OPEN");
So, my question would be how do I have to inherit the class to be able to pass and override the config parameters of the superclass?

EDIT: I added "Ext.apply(this, config); " to the Om.data.Orderstore function declaration but doesn't work...

2. Yes, I have baseParams initialized which are overwritten by the setFilterParameter() function. This works but on both instances. The full code is now like this (changed the extend call as described here: http://www.sencha.com/learn/Manual:Intro:Inheritance (http://www.sencha.com/learn/Manual:Intro:Inheritance%29:)


// DataStore for a complete order
Om.data.OrderDataStore = function(config) {
// call parent constructor
Om.data.OrderDataStore.superclass.constructor.call(this, config);
};
Ext.extend(Om.data.OrderDataStore, Ext.data.GroupingStore, {
proxy : new Ext.data.HttpProxy( {
url : 'data.php', // File to connect to
method : 'POST'
}),
baseParams : {
action : "LIST_ORDERS"
}, // this parameter asks for listing
reader : new Ext.data.JsonReader( {
root : 'results',
totalProperty : 'total',
id : 'id'
}, [ {
name : 'id',
type : 'int',
mapping : 'id'
}, {
name : 'ba_number',
type : 'int',
mapping : 'ba_number'
}, {
name : 'ordered_at',
type : 'date',
dateFormat : 'd.m.Y H:i:s',
mapping : 'ordered_at'
}, {
name : 'ordered_by',
type : 'string',
mapping : 'ordered_by'
}, {
name : 'product',
type : 'string',
mapping : 'product'
}, {
name : 'vendor',
type : 'string',
mapping : 'vendor'
}, {
name : 'order_code',
type : 'int',
mapping : 'order_code'
}, {
name : 'amount',
type : 'string',
mapping : 'amount'
}, {
name : 'current_amount',
type : 'string',
mapping : 'current_amount'
}, {
name : 'price_total',
type : 'float',
mapping : 'price_total'
}, {
name : 'cost_number',
type : 'int',
mapping : 'cost_number'
}, {
name : 'delivered_at',
type : 'date',
dateFormat : 'd.m.Y H:i:s',
mapping : 'delivered_at'
}, {
name : 'accepted_by',
type : 'string',
mapping : 'accepted_by'
}, {
name : 'storage_location',
type : 'string',
mapping : 'storage_location'
}, {
name : 'comments',
type : 'string',
mapping : 'comments'
} ]),
sortInfo : {
field : 'id',
direction : "ASC"
},
setFilterParameter : function (parameter) {
this.baseParams['filter'] = parameter;
}
});

Condor
13 Feb 2011, 3:41 AM
Do NOT specify complex objects (like proxy, baseParams, reader and sortInfo) in the prototype. Remember that they will be shared by all instances.

Instead, create these objects in the constructor (or initComponent if it is an Ext.Component).

Oliver Specht
14 Feb 2011, 4:35 AM
Hi,
I understand but why can't I overwrite the id in the constructor?

And generally:
Is this a general JS "problem" or only extJS specific?

Because normally I would expect two "baseParam" objects when creating two instances. I understood the prototyping like a class declaration e.g. in Java.

EDIT: What sense does it make then to inherit in ExtJs when I need to put the whole configuration in every instance declaration?!? Seems weird to me :)

Thanks for your help anyway!

Oliver

Condor
17 Feb 2011, 2:04 AM
I think you're missing the point.

I meant you should be using:

Om.data.OrderDataStore = Ext.extend(Ext.data.GroupingStore, {
constructor: function (config) {
Om.data.OrderDataStore.superclass.constructor.call(this, Ext.apply({
proxy: new Ext.data.HttpProxy({
url: 'data.php',
method: 'POST'
}),
baseParams: {
action: "LIST_ORDERS"
},
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
idProperty: 'id',
fields: [{
name: 'id',
type: 'int',
mapping: 'id'
}, {
name: 'ba_number',
type: 'int',
mapping: 'ba_number'
}, {
name: 'ordered_at',
type: 'date',
dateFormat: 'd.m.Y H:i:s',
mapping: 'ordered_at'
}, {
name: 'ordered_by',
type: 'string',
mapping: 'ordered_by'
}, {
name: 'product',
type: 'string',
mapping: 'product'
}, {
name: 'vendor',
type: 'string',
mapping: 'vendor'
}, {
name: 'order_code',
type: 'int',
mapping: 'order_code'
}, {
name: 'amount',
type: 'string',
mapping: 'amount'
}, {
name: 'current_amount',
type: 'string',
mapping: 'current_amount'
}, {
name: 'price_total',
type: 'float',
mapping: 'price_total'
}, {
name: 'cost_number',
type: 'int',
mapping: 'cost_number'
}, {
name: 'delivered_at',
type: 'date',
dateFormat: 'd.m.Y H:i:s',
mapping: 'delivered_at'
}, {
name: 'accepted_by',
type: 'string',
mapping: 'accepted_by'
}, {
name: 'storage_location',
type: 'string',
mapping: 'storage_location'
}, {
name: 'comments',
type: 'string',
mapping: 'comments'
}]
}),
sortInfo: {
field: 'id',
direction: "ASC"
}
}, config));
},
setFilterParameter: function (parameter) {
this.baseParams['filter'] = parameter;
}
});

Now it creates a separate instance of proxy, baseParams, reader and sortInfo for every store instance.

Oliver Specht
17 Feb 2011, 6:48 AM
Got it! Thank you very much for your help!