Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know if this is the case with your scenario, but it's worth a post.</p> <p>I had issues with the mapping plugin, when I had a more complex viewmodel with nested properties or lists. It turned out that after mapping to an already constructed viewmodel, the sub-objects were no more observables. With this issue, for me this code worked, what I've found somewhere (unfortunately I already really don't know where). I called this function for my viewmodel after mapping to that.</p> <pre><code>function makeAllObservables(observable) { // Loop through its children for (var child in observable()) { // If this child is not an observable and is an object if ((!ko.isObservable(observable()[child])) &amp;&amp; (typeof observable()[child] === "object")) { // Make it an observable observable()[child] = ko.observable(observable()[child]); // Make all of its children observables makeAllObservables(observable()[child]); } } }; </code></pre> <p>Usage (when updating the model from server response, the first line should not be there):</p> <pre><code>var model = ko.observable({}); ko.mapping.fromJS(myJSObject, {}, model); makeAllObservables(model); ko.applyBindings(model); </code></pre> <p>I ment mapping to an already constructed viewmodel for example, when you want to update your viewmodel with new JSON data from server. In that case I lost nested bindings without the code above.</p> <p>UPDATE: I've found the source where I borrowed the technique from, <a href="https://stackoverflow.com/questions/10555115/knockout-js-make-every-nested-object-an-observable">here</a>. Note that I slightly modified that code in that post, because somehow that was not working for me.</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