PDA

View Full Version : Record acts destructively on its default data



metalsiren
24 Jan 2011, 3:22 PM
I'm trying to provide a set of defaults for Records of a common type. I've done this by providing a defaults object that I pass into each Record's constructor call. Ideally, this would simply set the record up to mirror the default object and that would be the end of the interaction. However, the current behavior is surprising:


Ext.data.Record = function(data, id){

this.id = (id || id === 0) ? id : Ext.data.Record.id(this);
this.data = data || {};
};The record constructor currently uses the defaults object as the Record's data field. This means that if you create a Record r1 with a defaults object d, change r1, and then create a second record r2 with d for defaults, r2 will have the current values of r1! :O

The workaround is simple enough (pass a copy of your defaults, not the defaults object itself), but I think that the behavior should be changed or at the least, a warning should be placed in the Ext.data.Record documentation that details the potential trouble.

I'm on ExtJS 3.2.2, but I can see that this is a problem in 3.3.1 as well.

My solution is simple, but I provide it here in hopes of helping those who may come across this bug before it gets fixed:

var rt = this.getStore().recordType;
var rec = new rt(Ext.apply({}, this.recordDefaults)); // pass a copy of your defaults so they don't get clobbered