1

If i have defined a filter in my collection like this:

filterByEveryday: function(){
    return this.models.filter(
      function(c) { 
        return c.Everyday == true; 
      })
   }

How can I use that filter in my route to select just the everyday breakfasts?

my route looks like this (currently fetching all the foods)

  products: function(type){
    console.log('product' + type );
    Breakfast.foods = new Breakfast.Collections.Foods()

    new Breakfast.Views.Foods({ collection: Breakfast.foods }); 
    Breakfast.foods.fetch();
    console.log(Breakfast.foods.filterByEveryday().length)
  },

There are a few problems here.
1. Breakfast.foods.filterByEveryday().length returns 0. Even if i change the test to true. I guess I am using it wrong? 2. There seems to be a lot of logic in the route, is this normal for backbone? seems like it should be in the view, but I'm not sure how to refactor it and have per route filters.
3. trying to pass the filtered collection to the view like this:

collection: Breakfast.foods.filterByEveryday()

gives me

TypeError: Object [object Array] has no method 'bind' 

So thats wrong too i suppose.

4

1 に答える 1