WCF JSON Service 日期数据转换

原文:http://zianet.dk/blog/2011/06/03/dates-with-a-sencha-touch-data-model-and-a-wcf-json-service/

Dates With a Sencha Touch Data Model and a WCF JSON Service

Posted in  Sencha Touch, WCF by Kristian Kristensen on the June 3rd, 2011

I’ve been developing a Sencha Touch Mobile app recently, that interface with a WCF REST service exposing data via JSON. One of the issues I faced was parsing the dates returned from the service into a date type in the Sencha Touch Data Model. This post explains one way to do that.

Say we have a simple model that only holds an id and a date:

1 Ext.regModel("MyModel", {
2   fields : [
3     'id',
4     {name : 'MyDate', type : 'date'},
5   ]
6 });

When data is read into this model using a Proxy from a WCF REST JSON service, the MyDate column will just hold the string representation of the data such as “/Date(1238606590509+0800)/”, ie. it won’t be parsed. The trick to overcome this is to utilize the “convert” property that you can set on a field. This allows you to call out to a helper function every time a value will be set on the model for that field.
Now our model looks like this:

1 Ext.regModel("MyModel", {
2   fields : [
3     'id',
4     {name : 'MyDate', type : 'date', convert : toDateFromJSON},
5   ]
6 });

We need to define our converter function;

1 function toDateFromJSON(v, record) {
2   return Date.parseDate(v, "M$");
3 }

In this converter function we utilize the built in Date object. When giving it the format string “M$” it will parse Microsoft Serialized AJAX dates which is what’s used with WCF, REST and the JSON formatter OOB.

Now our model will have the correct datatype for the MyDate property, which will allow us to call useful functions on it such as format.


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值