I've an editable grid and a form to add data to the grid. User can edit the grid data directly and he can use the form section to enter new entry. I've had this working fine with ASP.Net MVC combined with ajax form. I updated the whole grid html upon successful ajax postback. Nothing complex, a traditional implementation.
Now, I've been working to enhance this with knockoutjs. The idea is to persist the UI but do everything in backend so that the user experience is better with least flickers and less traffic / postback time. As expected, everything goes in backend. Here's the summary -
I've a viewModel which consists of a 'commentToAdd' object and a 'commentList' object which is as expected a list of comment objects. I bind the 'commentList' object with my Grid and the 'commentToAdd' binds with the form.
var viewModel = new commentsModel();
$.getJSON('@Url.Action("CommentsKOVM", "Claim", new { ClaimGUID = commentObj.ClaimGUID })',
function(data) { //upon success
viewModel.commentToAdd = ko.mapping.fromJS(data.CommentToAdd);
viewModel.allComments = ko.mapping.fromJS(data.AllComments, mapping);// Initial items
ko.applyBindings(viewModel);
Works fine. But I've a 'date' field. JSON renders it as '/Date(1224043200000)' So, I've to format it - How do I format a Microsoft JSON date? like:
<span data-bind="text:new Date(parseInt(PostedOn.toString().substr(6))).toLocaleFormat('%d/%m/%Y')"></span>
But it doesn't work because it seems that 'PostedOn' is converted into an observable! The .toString returns a function definition!
I've also tried to implement the date binding as explained by Hanselman article.
I don't seem to get my date displayed correctly in the Grid. I also tried to 'ignore the mapping' :
var mapping = {'ignore': ["PostedOn"]};
but don't know how to make it work for my child object (i.e. commentList.PostedOn).
I might not be doing it in the right manner so pls suggest or atleast help me get a correct date in my Grid.
PS: Works as expected when I use ko.observableArray(data.commentList) but it doesn't go well with edit feature.