Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout.js how do I build up a string from a binding
    primarykey
    data
    text
    <p>I am creating a list of links using a knockout binding:</p> <p>The Javascript and view model looks like this:</p> <pre><code>$(function () { var adminViewModel = function() { var self = this; self.leftItems = ko.observable([ { Name: "Item1", Id: 0 }]); self.getChildren = function (id, list) { var url ="@Url.Content("~/api/Test/GetChildren/")" + id; $.getJSON(url, function (data) { list(data); }); }; } var Admin3App = window.Admin3App = window.Admin3App || {}; Admin3App.viewModel = new adminViewModel(); ko.applyBindings(Admin3App.viewModel); function getLeftChildren(id) { Admin3App.viewModel.getChildren(id, Admin3App.viewModel.leftItems); } getLeftChildren(0); } </code></pre> <p>(EDIT: the init of the <code>ko.observable</code> was missing an Id (even though this did not cause an error, added it)</p> <p>How this works is the view model will load a bunch of items that has no parent (id 0). For the sake of simplicity I only included the left version. But the page has a list of items on the left and right so there is a function for each.</p> <p>The left items is populated to view model left items and displayed as below. But each item is a link and once clicked via javascript will refresh the items based on the parent Id. (Much like browsing a folder viewer in explorer).</p> <p>But i cannot figure out how to declare the binding in knockout to build up the url. I know this is probably rediculously easy and I am just missing it.</p> <p>here is the html I have attempted amongst others</p> <pre><code>&lt;div class="leftView"&gt; &lt;div data-bind="foreach: leftItems"&gt; &lt;a data-bind="text: Name, attr : { href:'javascript:getLeftChildren(' + Id + ')' }"&gt;&lt;/a&gt;&lt;br /&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>(EDIT: missed a single quote, but got the same error anyway) But I keep getting a binding error</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.
    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