上载MVC 4处理日期,敲除绑定 [英] Upshot MVC 4 handling of Date, knockout bindings

查看:89
本文介绍了上载MVC 4处理日期,敲除绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

upshot.js如何处理DateTime对象?

How does upshot.js handles DateTime objects?

似乎它只是创建普通"字符串,而不创建真正的JS日期对象.

As it seems its just creating "normal" strings and not creating a real JS date object.

与JSON日期格式和MVC有关的所有问题已在此处讨论: 使用Asp.Net MVC和KnockoutJS处理日期

All the problems related to JSON date formatting and MVC are discussed here already: Handling dates with Asp.Net MVC and KnockoutJS

因此,即使将MVC的默认JSON格式器更改为JSON.Net,它至今仍未进行自动转换. 不幸的是,所有绑定必须手动"完成,以在内部创建Date对象.使用JSON.Net格式的日期转换为新的Date(string)方法似乎可以正常工作,因为构造函数可以很好地处理日期格式字符串.

So even after changing the MVC default JSON formatter to JSON.Net its still not doing an automatic conversion to date. Unfortunately all the bindings must be done "per hand" to create a Date object internally. With the JSON.Net formatted date to new Date(string) approach seems to work ok as the constructor is abel to handle the date formatstring perfect.

是否有一个通用"库可用于处理敲除绑定中的所有不同值类型?

Is there a "general" lib available for handling all the different value types in bindings for knockout?

推荐答案

这是一个古老的故事,因为json没有日期的默认格式.而且,.Net(.Net不仅仅是asp.net)假装的/Date(.....)/格式也不容易通过所有现代浏览器允许的json自定义进行处理:

This is an old story, due to the fact that json has no default format for dates. Moreover, the format /Date(.....)/ pretended by .Net (.Net not just asp.net) is not easy to handle also with the json customization allowed by all modern browsers:

  1. 第一个问题是浏览器json解析功能无法理解/Date(.....)/格式.通过自定义Json解析,可以轻松解决此问题.网上有很多补丁.
  2. 将日期发送回服务器时,您也会遇到相同的问题.但是,在这种情况下,修补非常困难,因为虽然JSON.stringify方法接受用于自定义json序列化的函数,但是如果传递的函数将每个日期转换为字符串"/Date(...)/",则该修补程序非常困难. ..然后使用字符串化函数ADD furteh \并将其转换为:"\/Date(.....)\/",即它转义了\ char ....但是这种方式转换的字符串不能被网络.另一方面,如果将日期转换为/Date(.....)/...,则不会添加转义字符,因此...在这种情况下,也会得到错误的编码.
  3. 如果您解决了上述问题,则在往返服务器/客户端/服务器之后,日期返回值将更改.具体来说,时间轴偏移量是从原始日期中减去的...这是由于.Net和浏览器处理时间轴的方式不同.
  4. 快照只是调用浏览器的JSON.stringify函数...因此它不给您自定义日期的空间.

如果您使用Mvc控件工具包项目的客户端阻止功能(我是的协调员),您可以使用自动处理问题1和2的增强型"剔除工具.在几天后的下一个发行版中,我还将添加对问题3的自动处理. 但是...这将无法解决upshot的问题...因为我钩住了upshot不使用的knokout映射插件的映射功能来将数据发送回服务器...并且因为upshot直接调用JSON.stringify ...似乎没有办法解决此问题...除了在发布数据时修改upshot以处理json自定义格式外.

if you use the client blocks feature of the Mvc Controls Toolkit project(I am the coordinator of) you can use an "enhanced" knockout that handles automatically problems 1 and 2. In the next release to come in a few days I will add also the automatic handling of problem 3. However...this WILL NOT SOLVE the problem of upshot...since I hooked the mapping functions of the knokout mapping plugin that are not used by upshot to send data back on the server...and since upshot calls directly JSON.stringify...there seems to be no way to fix the problem...other than modifying upshot to handle json custom formats when posting data.

在Client Blocks的upcomig版本中,我将提供一个UpdateManager类,该类的工作与可以正确处理日期的快照相似" ...但是,由于它使用完全不同的更新策略,因此永远不会替代upshot. ..and只是一个选择,不能替代. 因此,我希望在最终版本的最终结果中,可以自定义数据的json格式.

In the upcomig release of Client Blocks I will provide an UpdateManager class that do a job "similar" to upshot that handle properly dates...However this will never be a substitute for upshot since it uses a quite different update strategy...and is just an option MORE not a substitute. So I hope that in the final release of upshot there will be the possibility to customize the json formatting of data.

这篇关于上载MVC 4处理日期,敲除绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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