PDA

View Full Version : what does recordType() do with the first parameter?



bareflix
14 Jul 2010, 2:38 PM
I have created a direct store and added as one of it's properties:


defaultRec:
{
name: "new skill",
default_attribute: "Dexterity",
description: "desc goes here",
type: "easy"
}

when the user clicks an add button, I do:


var store = this.list.getStore();
var rec = new store.recordType(store.defaultRec);
store.addSorted(rec);
this.form.loadRecord(rec);

Now the user goes off, edits the form, saves, then hits the add button again. This time the defaultRec no longer contains my default data, but it has what the user entered in the form.

Is that the expected behavior? I expected recordType to copy the data into the newly created record.
any ideas?

--
Chris

BitPoet
15 Jul 2010, 7:54 AM
The record's constructor stores a reference to the data, so modifying the data object also modifies the record's contents. In your case all records reference the same data object.

One usually circumvents that by either explicitely creating a copy:


var rec = new store.recordType(Ext.apply({}, store.defaultRec));
or (IMHO nicer) use a retrieval function instead of an object property:


getDefaultRec: function() {
return {
name: "new skill",
default_attribute: "Dexterity",
description: "desc goes here",
type: "easy"
}
}
/* ... and further down ... */
var rec = new store.recordType(store.getDefaultRec());