PDA

View Full Version : [FIXED-247] Model date fields do not parse



JacobGu
27 Aug 2010, 9:46 AM
In the following code, we would expect myTestDate to contain a date object. But instead it contains the original string.


Ext.regModel('TestModel', {
fields: [
{name: 'testdate', type: 'date', dateFormat: 'Y-m-d'},
]
});
var myTestModel = Ext.ModelMgr.create({testdate: '2007-01-10'}, 'TestModel');
var myTestDate = myTestModel.get('testdate');

JacobGu
27 Aug 2010, 11:31 AM
Based on my understanding, it looks like the problem is that data types are not converted by the Model's constructor. They are only converted through a Reader. However, your documentation implies that the Model constructor is supposed to convert. I edited the constructor as follows by adding the three lines of code in red, and it works:


constructor: function(data, id) {
data = data || {};

if (this.evented) {
this.addEvents(

);
}

//add default field values if present
var fields = this.fields.items,
length = fields.length,
field, name, i;

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

if (data[name] == undefined) {
data[name] = field.defaultValue;
}
else {
data[name] = field.convert(data[name]);
}
}

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

this.data = data;

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

Ext.data.Model.superclass.constructor.apply(this);

On another note, I tried doing an override of Ext.data.Model constructor from my own code, but it doesn't work. I tried Ext.override, also tried directly setting Ext.data.Model.constructor and Ext.data.Model.prototype.constructor. Nothing works without editing extjs file. Can constructor be changed from my own code? Am I missing something?

JacobGu
30 Aug 2010, 4:08 AM
My working fix in order to avoid hacking the Ext library itself is to put the following in my code:


Ext.ModelMgr.create = function(config, name) {
var con = typeof name == 'function' ? name : this.types[name || config.name];

var model = new con(config);

//
// NEW CODE BELOW TO PATCH BUG IN Ext.data.Model constructor,
// which fails to convert data to specified field type.
//

//add default field values if present
var fields = model.fields.items,
length = fields.length,
field, name, i;

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

model.data[name] = field.convert(model.data[name]);
}

return model;
};

TommyMaintz
2 Sep 2010, 2:50 PM
This has been fixed, and will be part of today's 0.94 release.