Note that there are some explanatory texts on larger screens.

plurals
  1. POActivating knockout bindings to specific part of document
    primarykey
    data
    text
    <p>Consider this code:</p> <pre><code> $(document).ready(function () { var menuView = function() { var self = this; this.blancos = ko.observableArray([]); this.load_blancos = function() { $.getJSON("{% url api_template_list_create %}", function (data) { ko.mapping.fromJS(data, {}, self.blancos); }) }; this.init = function() { self.load_blancos(); }; this.init(); }; ko.applyBindings(new menuView(), document.getElementById('blancos_menu')); }); </code></pre> <p>And this HTML:</p> <pre><code> &lt;ul class="dropdown-menu" id="blancos_menu"&gt; &lt;li&gt;&lt;a href="{% url template_planning %}"&gt;Create new&lt;/a&gt;&lt;/li&gt; &lt;li class="divider"&gt;&lt;/li&gt; &lt;span data-bind="text: $root.blancos"&gt;&lt;/span&gt; &lt;!-- ko foreach: $root.blancos --&gt; &lt;li&gt; &lt;a href="#" data-bind="text:name"&gt;&lt;/a&gt; &lt;/li&gt; &lt;!-- /ko --&gt; &lt;/ul&gt; </code></pre> <p>The ko: foreach function does not execute (i.e. no list items generated). I do see "[object Object],[object Object]" in the preceding span (there for testing purposes). </p> <p>For a very bizarre reason, when I change the line <code>&lt;span data-bind="text: $root.blancos"&gt;&lt;/span&gt;</code> to <code>&lt;span data-bind="text: blancos"&gt;&lt;/span&gt;</code>, the ko: foreach function is executed and I get to see the different list items. </p> <p>Surprisingly I do get a JavasScript error stating that blancos is not defined and hence cannot be bound. (BUT! The span still contains the text "[object Object],[object Object]").</p> <p>I really can't get my around it; thanks for looking into this.</p> <p>By the way, the data returned by the JSON call is:</p> <pre><code>[{"id": 1, "name": "123"}, {"id": 2, "name": "test_wzzob"}] </code></pre> <p><strong>UPDATE</strong></p> <p>Changing</p> <pre><code>ko.mapping.fromJS(data, viewModel); </code></pre> <p>into</p> <pre><code>ko.mapping.fromJS(data, {}, self.blancos); </code></pre> <p>does the trick indeed, but there's still an error</p> <pre><code>Uncaught Error: Unable to parse bindings. Message: ReferenceError: blancos is not defined; Bindings value: foreach: blancos </code></pre> <p>The same applies for other properties or methods. I <em>dont't</em> get that error when prepending the variables (or functions) I'm binding in the HTML with $root (but then, they're empty).</p>
    singulars
    1. This table or related slice is empty.
    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.
    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