PDA

View Full Version : Default Value of a Model Field is not cloned



tomb@ibcos.co.uk
24 Jun 2011, 1:08 AM
REQUIRED INFORMATION


Ext version tested:

Ext 4.0.2


Description:

If a default value of a field on a model is an object, the object is referenced for every model instance.


HELPFUL INFORMATION

Possible fix:

Clone the default value. Change highlighted below.

Constructor from Ext.data.Model

// raw not documented intentionally, meant to be used internally.
constructor: function(data, id, raw) {
data = data || {};

var me = this,
fields,
length,
field,
name,
i,
isArray = Ext.isArray(data),
newData = isArray ? {} : null; // to hold mapped array data if needed

/**
* An internal unique ID for each Model instance, used to identify Models that don't have an ID yet
* @property internalId
* @type String
* @private
*/
me.internalId = (id || id === 0) ? id : Ext.data.Model.id(me);

/**
* The raw data used to create this model if created via a reader.
* @property raw
* @type Object
*/
me.raw = raw;

Ext.applyIf(me, {
data: {}
});

/**
* Key: value pairs of all fields whose values have changed
* @property modified
* @type Object
*/
me.modified = {};

// Deal with spelling error in previous releases
if (me.persistanceProperty) {
//<debug>
if (Ext.isDefined(Ext.global.console)) {
Ext.global.console.warn('Ext.data.Model: persistanceProperty has been deprecated. Use persistenceProperty instead.');
}
//</debug>
me.persistenceProperty = me.persistanceProperty;
}
me[me.persistenceProperty] = {};

me.mixins.observable.constructor.call(me);

//add default field values if present
fields = me.fields.items;
length = fields.length;

for (i = 0; i < length; i++) {
field = fields[i];
name = field.name;

if (isArray){
// Have to map array data so the values get assigned to the named fields
// rather than getting set as the field names with undefined values.
newData[name] = data[i];
}
else if (data[name] === undefined) {
data[name] = Ext.clone(field.defaultValue);
}
}

me.set(newData || data);
// clear any dirty/modified since we're initializing
me.dirty = false;
me.modified = {};

if (me.getId()) {
me.phantom = false;
}

if (typeof me.init == 'function') {
me.init();
}

me.id = me.modelName + '-' + me.internalId;
}