Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The proper way to filter a backbone.collection by using Marionette.CompositeView

I have a Marionette.CompositeView which needs to render a collection.
I would like to filter this collection on fetch and add action.
I tried with the following code (1) but I get the following error (2).

Any ideas, thanks.


(1)

var myCompositeView = Marionette.CompositeView.extend({

    initialize: function () {
        this.collection = app.taskCollection.where({type: 'todo'});
    }

});

(2)

// Uncaught TypeError: Object  has no method 'on'
like image 301
Lorraine Bernard Avatar asked Sep 27 '12 09:09

Lorraine Bernard


1 Answers

Marionette's CompositeView and CollectionView both expect the collection setting to be a valid Backbone.Collection. The where method on Backbone's collection does not return a Backbone.Collection, it return an array. So you have to wrap a collection around the results:


initialize: function(){
  var filtered = app.taskCollection.where({type: 'todo'});
  this.collection = new Backbone.Collection(filtered);
}

Of course you can use any type that extends from Backbone.Collection. I just wanted to illustrate the point of it being a collection with this example.

like image 145
Derick Bailey Avatar answered Oct 20 '22 04:10

Derick Bailey