Multiple collections tied to one base collection with filters and eventing

Posted by damienc88 on Stack Overflow See other posts from Stack Overflow or by damienc88
Published on 2013-06-25T06:55:17Z Indexed on 2013/06/26 4:21 UTC
Read the original article Hit count: 146

I have a complex model served from my back end, which has a bunch of regular attributes, some nested models, and a couple of collections.

My page has two tables, one for invalid items, and one for valid items. The items in question are from one of the nested collections. Let's call it baseModel.documentCollection, implementing DocumentsCollection.

I don't want any filtration code in my Marionette.CompositeViews, so what I've done is the following (note, duplicated for the 'valid' case):

var invalidDocsCollection = new DocumentsCollection(
     baseModel.documentCollection.filter(function(item) {
            return !item.isValidItem();
     })
);

var invalidTableView = new BookIn.PendingBookInRequestItemsCollectionView({
    collection: app.collections.invalidDocsCollection
});

 layout.invalidDocsRegion.show(invalidTableView);

This is fine for actually populating two tables independently, from one base collection. But I'm not getting the whole event pipeline down to the base collection, obviously. This means when a document's validity is changed, there's no neat way of it shifting to the other collection, therefore the other view.

What I'm after is a nice way of having a base collection that I can have filter collections sit on top of. Any suggestions?

© Stack Overflow or respective owner

Related posts about backbone.js

Related posts about backbone.marionette