Note that there are some explanatory texts on larger screens.

plurals
  1. POko.mapping.fromJS and Date objects
    text
    copied!<p>I've asked questions about knockout in the past but haven't had a chance to work on knockout since the last question was addressed. My latest question is specifically about the use of ko.mapping.fromJS where a Date object is involved.</p> <p>When I use ko.mapping.fromJS to map an object that has a nested Date object that Date object gets translated to...I don't know what it is. </p> <p>Here is my jsfiddle: <a href="http://jsfiddle.net/L5sgW/46/" rel="nofollow">http://jsfiddle.net/L5sgW/46/</a></p> <p>HTML:</p> <pre><code>&lt;p&gt; &lt;span&gt;Name:&lt;/span&gt; &lt;span data-bind="text: IntroData.Name"&gt;&lt;/span&gt; &lt;button id="update" data-bind="click: Update"&gt;Update!&lt;/button&gt; &lt;/p&gt; </code></pre> <p>Javascipt:</p> <pre><code>var ViewModel = function (data) { var me = this; ko.mapping.fromJS(data, {}, me); me.Update = function () { alert(stuff2.IntroData.TestDate.toString('yyyy-MM-dd')); ko.mapping.fromJS(stuff2, {}, me); alert(me.IntroData.TestDate.toString('yyyy-MM-dd')); }; return me; }; var stuff = { IntroData: { Name: 'Test' } }; var stuff2 = { IntroData: { TestDate: new Date('2013-09-01'), Name: 'Test2' } }; window.viewModel = ko.mapping.fromJS(new ViewModel(stuff)); ko.applyBindings(window.viewModel); </code></pre> <p>When you click the "Update!" button "Stuff2.IntroData.TestDate" is displayed in the alert. Then the mapping occurs. When the alert for "me.IntroData.TestDate" happens it results in: "function d(){if(0 <p>Why doesn't the date value get preserved? How do I preserve it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload