PDA

View Full Version : model reader cannot read local stored date (localStorageProxy, dateFormat)



shubakk
23 Nov 2011, 2:10 AM
Hi,

I found an annoying bug while working on localStorage with date fields.
I want to store records with a date field :


Ext.regModel('dateTest', {
idProperty: 'id',
proxy : {
type: 'localstorage',
id: 'datetest'
},
fields: [{
name: 'id',
type: 'int',
useNull: true
},{
name: 'myDate',
type: 'date',
dateFormat: 'Y-m-d'
}]
});

Ext.regStore('syncStore', {
model: "dateTest"
});

var syncStore = Ext.getStore('syncStore');

syncStore.add({
id: 1,
myDate: '2011-11-23'
});

syncStore.each(function(item){
console.log("syncStore | %s : %o",item.get("id"),item.get("myDate"));
});

syncStore.sync();

The console.log line gives :

syncStore | 1 : Wed Nov 23 2011 00:00:00 GMT+0100 (CET)

Now if i reload my page, or go to another page and try to load the records :

Ext.regModel('dateTest', {
idProperty: 'id',
proxy : {
type: 'localstorage',
id: 'datetest'
},
fields: [{
name: 'id',
type: 'int',
useNull: true
},{
name: 'myDate',
type: 'date',
dateFormat: 'Y-m-d'
}]
});


Ext.regStore('loadStore', {
model: "dateTest"
});

Ext.getStore('loadStore').load({
callback: function(records, operation, success){
Ext.each(records,function(item){
console.log("loadStore | %s : %o",item.get("id"),item.get("myDate"));
});
}
});

It gives me :

loadStore | 1 : undefined

It fails to read the date field.

If I take a look into the resources view of the developer tool, I can see my record :

{"id":1,"myDate":"2011-11-23T00:00:00"}
The date is not format with the dateFormat of the field.

A quick fix is to make a convert function on the dateField :

{
name: 'myDate',
type: 'date',
dateFormat: 'Y-m-d',
altFormat: 'Y-m-dTH:i:s',
convert : function(value, rec){
if(Ext.isEmpty(value) === true){
return null;
}

var formatValue = Date.parseDate(value,this.dateFormat);
if(Ext.isEmpty(formatValue) === true){
formatValue = Date.parseDate(value,this.altFormat);
}
return formatValue;
}
}

I've made a longer post on the same bug on ExtJS 4 : http://www.sencha.com/forum/showthread.php?156777-model-reader-cannot-read-local-stored-date-%28localStorageProxy-dateFormat%29

mitchellsimoens
23 Nov 2011, 11:33 AM
When the bug has been confirmed and fixed in Ext JS 4, it will most likely make it over to ST2 but development on ST1 is no longer kept up with as ST2 is going to be the active version.