2

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?

Community
  • 1
  • 1
Obiwan007
  • 646
  • 1
  • 8
  • 20
  • I found another issue here with JSON.net formatted dates. If I user the replaced JSON.Net formatter the date loogs good and could easily converted to JS date objects. BUT if you like to send the entity back to the server - it fails. Somehow it seems that the desrialisation does not work if the formatter replaced the defaultjson formatter... – Obiwan007 Mar 27 '12 at 11:42

1 Answers1

3

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.

Francesco Abbruzzese
  • 4,139
  • 1
  • 17
  • 18
  • Thx for your reply. 1. problem is somehow easily solvable - I just use the JSON.Net lib to create the date in ISO format (not /Date*). So the new Date(string) is working perfect. 2. sending back is an issue as you pointed out. I have currently no Idea what the client is posting back to the server. I check that with fiddler sometime :) Im looking forward to your lib. At the moment upshot/SPA is not really an option as there are too many issues so far. Maybe I'll try the direct approach in creating the model via the modelbuilder of knockout. – Obiwan007 Mar 27 '12 at 18:03
  • See this post of Scott Hanselman; http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx. At the end of the post he promise that in the final release...they will ship JSON.net with WebApi...so there will be a "normalization" in all js and dll...that will work together – Francesco Abbruzzese Mar 27 '12 at 23:04
  • my lib has been published on cedeplex see here: http://mvccontrolstoolkit.codeplex.com/ At that link you will find pointers to code samples and tutorials. Th official doc will be updated in the next days – Francesco Abbruzzese Apr 04 '12 at 14:50