ko.toJSON() 是否适用于日期? [英] Does ko.toJSON() work with dates?

查看:27
本文介绍了ko.toJSON() 是否适用于日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net mvc 页面上使用knockoutjs.我正在使用 ajax 通过调用 ko.toJSON(viewModel) 将表单持久化回服务器,然后使用 jQuery 将结果发布回服务器.除了作为空对象持久化的 Javascript 日期之外,视图模型上的所有属性都已成功序列化.

I am using knockoutjs on an asp.net mvc page. I am using ajax to persist a form back to the server by calling ko.toJSON(viewModel) and then posting the results back to the server using jQuery. All of the properties on the view model are successfully serialized except for the Javascript date which is persisted as an empty object.

声明:

var viewModel = {
    startTime: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

保存数据:

var postData = ko.toJSON(viewModel); 
$.ajax({
    url: "/data",
    type: "POST",
    data: postData,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function () {
        console.log('success!');
    },
    error: function () {
        console.log('fail!');
    }
});

viewModel.startTime() 的 console.log 值为:

The console.log value of viewModel.startTime() is:

日期 {2011 年 5 月 10 日星期二 11:30:00 GMT-0500(中部夏令时间)}

Save Data的第1行之后,postData的值为:

After line 1 of Save Data, the value of postData is:

{
    "startTime": {},
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

如果我将 Save Data 的第 1 行扩展到

If I expand line 1 of Save Data to

var jsonEvent = ko.toJS(viewModel);
jsonEvent.startTime = viewModel.startTime();
var postData = JSON.stringify(jsonEvent);

postData 的值为:

The value of postData is:

{
    "startTime": "2011-05-10T16:30:00.000Z",
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

谁能解释一下可能发生的事情以及我如何能够让knockoutjs 来处理日期对象?

Can anyone explain what might be going on and how I might be able to get knockoutjs to handle the date object?

推荐答案

鉴于 ko.toJS 和日期的当前问题,一种选择是创建一个dependentObservable,其中包含您希望服务器处理的真实值.

Given the current issue with ko.toJS and dates, one option would be to create a dependentObservable containing the real value that you want the server to deal with.

类似于:

var viewModel = {
    startTimeForInput: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

viewModel.startTime = ko.dependentObservable(function() {
    return this.startTimeForInput().toJSON();
}, viewModel);

ko.applyBindings(viewModel);

现在,当您调用 ko.toJSON 时,您将获得具有服务器可以使用的正确值的 startTime.

Now, when you call ko.toJSON you will get the startTime with the correct value that the server could use.

对于较旧的浏览器,诸如 json2.js 之类的内容将包含 Date 对象的 .toJSON.

For older browsers, something like json2.js would include the .toJSON for Date objects.

这篇关于ko.toJSON() 是否适用于日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆