I want to convert the timestamp to customized date format right after the server returns the data.
I tried to use the "convert" in Ext.data.field : http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-convert
But I cannot make it right. This is my model.
Ext.define('AM.model.Comment',{
extend: 'Ext.data.Model',
fields: [
{ name: 'createdTime', type: 'date', convert:function(v,record){record.parseDate(v,record);}}, // datetime
],
proxy: {
type: 'rest',
url:'../comments',
writer:{
type:'json'
},
reader: {
type: 'json'
}
},
parseDate:function(v,record){
console.log(v); //show 1347465600000
console.log(Ext.Date.format(new Date(v), 'Y-m-d')); //show 2012-09-13
return Ext.Date.format(new Date(v), 'Y-m-d');
}
});
After loading, I checked firebug and found the field "createdTime" is "undefined".
Can someone point out my mistake? Thanks!
I can achieve that without using "convert", just use Ext.Date.format(new Date(v), 'Y-m-d') in other component. But I think it will be better to do that in model. Then every component can always read the right date format as querying it.
解决方案
I found a solution. Instead of using "convert", I override the getData() method of reader.
Ext.define('AM.model.Comment',{
extend: 'Ext.data.Model',
fields: [
{ name: 'createdTime', type: 'datetime'},
],
proxy: {
type: 'rest',
url:'../comments',
writer:{
type:'json'
},
reader: {
type: 'json',
getData:function(data){
for(i = 0; i < data.length; i++){
data[i].createdTime = Ext.Date.format(new Date(data[i].createdTime), "Y-m-d");
}
return data;
}
}
}
});
If anyone has better solution, please tell me. And I still wonder why "convert" didn't work.If anyone know the reason, please tell me, too. Thanks!