jquery - Knockout.js, mapping plugin and moment.js - formatting/mapping json dates -


i using knockout.js mapping plugin. getting json data , using mapping plugin map html.

in json data json formatted date need map html using mapping plugin. possible use moment.js format date , allow mapping plugin map html? how json date, reformat readable date , map html?

// here json formatted date "duedate":"\/date(1362124800000)\/"  // here's data model var viewmodel; $.getjson('/myjsondata', function (data) {     viewmodel = ko.mapping.fromjs(data);     ko.applybindings(viewmodel);  // moment.js format date json - how can passed ko.mapping?     var mo = moment("\/date(1362124800000)\/").format("mmm yy"); }); 

here's alternative answer, utilizes custom binding. use in view this:

<span data-bind="textualdate: duedate"></span> 

the custom binding code this:

ko.bindinghandlers.textualdate = {     update: function(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext) {         var valueunwrapped = ko.utils.unwrapobservable(valueaccessor());         var textcontent = moment(valueunwrapped).format("mmm yy");         ko.bindinghandlers.text.update(element, function() { return textcontent; });     } }; 

this convenient because can use binding date observables, not duedate. example, suppose model gets extended other dates, can without having modify view model:

<span data-bind="textualdate: startdate"></span> <span data-bind="textualdate: reviseddate"></span> <span data-bind="textualdate: duedate"></span> <span data-bind="textualdate: wewillgetsuedpassedthisduedate"></span> 

you can check out this jsfiddle working demo.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -