Handling dates with Asp.Net MVC and KnockoutJS

前端 未结 8 1221
滥情空心
滥情空心 2020-12-08 04:56

I recently started working with KnockoutJs and quickly realized using the default Json(myModelWithADate) resulted in the default json encoding of \\/Date(

8条回答
  •  一个人的身影
    2020-12-08 05:18

    I would suggest a middle man approach through ko.mapping.fromJS( data, mapping ) this would allow you to customize even with a user defined object.

    var $data = { _ID : '1', _Created : someDate };  
    var $mapping = {
        '_Created' : {
           update: function (options) {
               return convertdata( options.data );
           }
        }
    }
    var viewDataModel = ko.mapping( data, mapping );  
    ko.applyBindings( viewDataModel );
    

    mapping parameter allows you handle changes easily and can easily be leveraged with arrays also.

提交回复
热议问题