Upshot MVC 4 handling of Date, knockout bindings

2020-07-24 06:06发布

How does upshot.js handles DateTime objects?

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

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

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?

1条回答
地球回转人心会变
2楼-- · 2020-07-24 06:15

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. The first problem is that the /Date(.....)/ format is not understood by the browser json parsing function. This is easily resolved by customizing Json parsing. There are a lot of patches on the net.
  2. You have the same problem when sending back the date to the server. However in this case it is very difficult to patch because while the JSON.stringify method accept a function to customize json serialization, if you pass a function that transform each date into the string "/Date(.....)/"...then the stringify function ADD furteh \ and transform it into: "\/Date(.....)\/", that is it escapes the \ char....but the string transformed this way is not understood by dotnet. On the other side if you transform dates into /Date(.....)/...no escape char is added so...also in this case you get a wrong encoding.
  3. Also if you solve the above problems...after a round trip server/client/server the date returns changed. Specifically the timeline offset is subtracted from the original date...This is due to the different ways .Net and browsers handle Timelines.
  4. upshot just calls the browser JSON.stringify function...so it leave you no room for customizing dates.

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.

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.

查看更多
登录 后发表回答