

Backbone.js collection filtering - dlikhten
https://plus.google.com/101713581467719669823/posts/gCddKVuwZwG

======
rgarcia
Unless I'm missing something, this seems to be re-inventing something that
Underscore already gives you on Backbone collections, _.each():

    
    
      var mycollection = new MyCollection();
      ...
      mycollection.each(function(model) {
        model.set({'passesFilterXYZ': filterXYZ(model)});
      });
    

EDIT: it also makes an entirely new collection instance representing the set
of filtered models...this seems terribly inefficient compared to the above.

~~~
dangrossman
I was hoping he had implemented filtering and paging by fetching and caching
the required objects from the server (overloading Backbone.Collection.fetch or
Backbone.Sync). Instead this is just slicing the arrays different ways; to
paginate/filter with it you still fetch 1000 models and only show 10 at a
time, or whatever option you pass.

~~~
dlikhten
See: <http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern>

Maybe I am citing the wrong pattern here. Basically each collection "filter"
will do it's own work and pretend like it in itselfis a collection. The
pagination is like a collection, but filtered in some way, the filtering is
too. Eventually I can easily implement another in the chain to fetch records
when I page to the page containing the record (say the record really only
contains the ID) and it would be pretty trivial to do.

Also lets you have a master collection of data, and multiple views doing
different things with that master collection, without constantly changing the
master.

