admin管理员组

文章数量:1402321

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'

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'
Share Improve this question asked Sep 27, 2012 at 9:18 Lorraine BernardLorraine Bernard 13.5k23 gold badges85 silver badges138 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: javascriptThe proper way to filter a backbonecollection by using MarionetteCompositeViewStack Overflow