extjs string类型转date_如何在ExtJS4.1模型中将时间戳转换为自定义日期格式?

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值