kmalakoff/knockback

multiple CollectionObservables to the same Backbone Collection

Closed this issue · 2 comments

Can multiple CollectionObservables be bound to the same Backbone collection? In my case I have a singleton collection that contains the data, and I want to bind to it with different instances of kb.collectionObservable. Is this possible?

Seems I traced it down to this line in the debug library line 1522-

!collection || (collection instanceof kb.Collection) || _throwUnexpected(this, 'not a collection');

So what I am seeing is that even if I have a backbone collection like this

var Collection= Backbone.Collection.extend({
    model: ModelObj,
    url: '/ist/rest/ModelObjs'
});

Even if I clone this collection or create a new Collection(), knockback will not bind to the new instantiation? Maybe knockback is modifying the prototype so that every new or cloned instantiation returns true for instanceof?

You can definitely have multiple collectionObservable, with different filters and sort options:

var panels = new Backbone.Collection([ { dock: 'left', order: 0 }, { dock: 'right', order: 1 }, ..... ]);

var panelsLeft = kb.collectionObservable(panels, {
filters: function (panel) { return panel.get("dock") != "left"; },
sort_attribute: "order"
});

var panelsRight = kb.collectionObservable(panels, {
filters: function (panel) { return panel.get("dock") != "right"; },
sort_attribute: "order"
});

var panelsExpandedLeft = kb.collectionObservable(panels, {
filters: function (panel) { return !panel.get("expanded") || panel.get("dock") != "left";
}});

var panelsExpandedRight = kb.collectionObservable(panels, {
filters: function (panel) { return !panel.get("expanded") || panel.get("dock") != "right";
}});

If you want to SHARE viewmodels, you have to use shareOptions:

http://kmalakoff.github.io/knockback/doc/classes/kb/CollectionObservable.html#shareOptions-instance