Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout maping.fromJS for observableArray from json object. Data gets lost
    primarykey
    data
    text
    <p>I have a view model with an observable and an observableArray. A button click is suppose to populate a JSON object and map it to the view model...which I believe "should" populate the observableArray. The button click happens. No errors. If I run through the debugger when the mapping occurs the obserable object gets populated but the obserableArray does not. Which means that the HTML objects that are suppose to display based on the observableArray do not get updated. </p> <p>HTML:</p> <pre><code>&lt;div data-bind="if: ErrorMessages().length"&gt; &lt;span&gt;Error Messages&lt;/span&gt; &lt;ul data-bind="foreach: ErrorMessages"&gt; &lt;li&gt; &lt;span data-bind="text: Description"&gt;&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;button data-bind="click: ShowErrors"&gt;Go&lt;/button&gt; var ViewModel = function () { var me = this; me.AData = ko.observable(); me.ErrorMessages = ko.observableArray([]); </code></pre> <p>Javascript:</p> <pre><code> me.ShowErrors = function () { var testdata = { AData: { Something: 1 }, ErrorMessages: [{ Description: 'Error #1', Code: '01' }, { Description: 'Error #2', Code: '02' }] }; var jsonData = ko.toJSON(testdata); ko.mapping.fromJS(jsonData, {}, me); }; }; ko.applyBindings(new ViewModel()); </code></pre> <p><a href="http://jsfiddle.net/Eves/v9SEk/4/" rel="nofollow">http://jsfiddle.net/Eves/v9SEk/4/</a></p> <p>My real code reaches out and gets data from a web service. I figure the above creating an object and converting it to JSON and then mapping it using ko.mapping.fromJS best represents what is happening. Anyways...</p> <p>I suspect I've done something stupid, or at least, assumed something I should not have.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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