I decided to take this approach:
My viewmodel has the JavaScriptDateTimeConverter on the DateTime property
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime TheEndOfTheWorld { get; set; }
My controller returns a JsonNetResult
return new JsonNetResult()
{
Data = new MayaCalendar();
};
And finally, my javascript parses this to
$.ajax({
url: "/Calendars/Maya",
dataType: "text",
}).done(function (data) {
var json = eval("(" + data + ")");
var viewmodel = new WeirdCalendarViewModel(json);
ko.applyBindings(viewmodel);
});
In short:
I'm using JavaScriptDateTimeConverter to return an invalid JSON string, that contains new Date(7919938800000) instead of "/Date(7919938800000)/"
I call the method with $.ajax, dataType "text" so jQuery won't try to parse the invalid JSON
I do the parsing myself, using eval(), before I pass the object to my knockout viewmodel.
Lunch
Pro:
- The knockout mapper gets to work with true javascript dates, instead of strings
Con:
- eval()