vintorg
vintorg

Reputation: 217

How to specify filtered Backbone collection for Marionette view

I have a Marionette composite view that displays a collection, which I set in my Application start handler:

App.on('start', function() {
  Backbone.history.start({pushState: true});

  // I load up this.appsCollection in my before:start handler
  var tblView = new this.appsTableView({
    collection: this.appsCollection
  });

  this.regions.main.show(tblView);
}); 

This works as expected, displaying my entire collection. In my models, I have a state field, and I want to display only models with state 0. I tried:

collection: this.appsCollection.where({state: 0})

but that doesn't work. I actually want to display states in 0 and 1, but I'm trying to just display state in 0 for right now.

What am I missing?

Upvotes: 0

Views: 94

Answers (2)

vintorg
vintorg

Reputation: 217

I was able to override the filter method in my Marionette CompositeView:

http://marionettejs.com/docs/v2.4.3/marionette.collectionview.html#collectionviews-filter

Upvotes: 0

billjamesdev
billjamesdev

Reputation: 14642

The problem probably resides in that .where() doesn't return a collection, but an array. http://backbonejs.org/#Collection-where This was supposedly to maintain compatibility with underscore.

If you change the line to:

collection: new Backbone.Collection( this.appsCollection.where( { state: 0 } ))

Does that help?

Upvotes: 3

Related Questions