Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout adding computed breaks observable array
    text
    copied!<p>Hi I am trying to use a computed value to make a filtered version of an observable array as per the example here <a href="http://knockoutjs.com/documentation/fn.html" rel="nofollow">http://knockoutjs.com/documentation/fn.html</a> ( right at the bottom).</p> <p>For the html I have have</p> <pre><code>&lt;select width="50px" data-bind="options: $root.resources, optionsText: 'Name'"&gt;&lt;/select&gt; &lt;select width="50px" data-bind="options: $root.available, optionsText: 'Name'"&gt;&lt;/select&gt;​ </code></pre> <p>And my viewModel looks like this:</p> <pre><code>var viewModel = function() { var self = this; self.resources = ko.observableArray([{Name: "Anna",Available: true}, {Name: "Bert", Available: false}]); self.getFilteredResources = function (isAvailable) { var all = self.resources(), results = []; var resource; for (resource in all){ if (resource.Available() === isAvailable){ results.push(resource); } } return results; }; //self.available = ko.computed(function() { self.getFilteredResources( true);}, this); }; ko.applyBindings(new viewModel());​ </code></pre> <p>You can also see the code here <a href="http://jsfiddle.net/patrickhastings/eCtFY/1/" rel="nofollow">http://jsfiddle.net/patrickhastings/eCtFY/1/</a></p> <p>As it stands the out put is one dropdown with Anna and Bert in it and one empty which is fine. </p> <p>When I uncomment out the line to declare self.available instead of the second dropdown being populated with Anna, I get two empty drop downs. Help Please tell me where I am being dumb.</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