Note that there are some explanatory texts on larger screens.

plurals
  1. POTracking list overlaps in knockoutjs
    primarykey
    data
    text
    <p>I have an application where objects might be in one of several lists. The UI for one list needs to mark each item as being on one of the <em>other</em> lists.</p> <p>A grossly simplified version of what I've implemented is this:</p> <pre><code>function Item(id, panel) { var that = this; this.id = id; this.viewModel = panel; this.isSelected = ko.computed(function(){ return panel.isSelected(that); }); } function Panel() { var that = this; //my two lists this.all = ko.observableArray(); this.selected = ko.observableArray(); this.isSelected = function(item){ return _(that.selected()).find(function(thing){ console.log("iterating"); return thing.id == item.id; }); }; } var panel = new Panel(); ko.applyBindings(panel); //add some things to the list for(var i=0; i&lt;40; i++){ var item = new Item(i, panel) panel.all.push(item); //let's select some of them. note it's actually a different object if (i % 2 == 0){ panel.selected.push(new Item(i, panel)); } }; ​ </code></pre> <p>Fiddle is here: <a href="http://jsfiddle.net/89j52/5/" rel="nofollow">http://jsfiddle.net/89j52/5/</a></p> <p>So, beyond the weirdness of giving the item a reference out to the panel, it also performs terribly. That's because every time you add another item to the selected list, it recomputes the selected status for <em>all</em> the items; notice how many times it prints "iterating". I understand why it's doing that, but it's painful.</p> <p>Now obviously, it doesn't <em>really</em> need to check every item each time I do an add. I could, for example, stash a lookup table for the items and then just update the right ones when something gets added to the selected list. But I'm not sure how to marry that to Knockout's observable/computed stuff and make the UI update seemlessly. How should I tackle this within Knockout's idioms?</p>
    singulars
    1. This table or related slice is empty.
    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