Friday, 30 August 2013

How to use the original Backbone collection after filtering?

How to use the original Backbone collection after filtering?

I'm relatively new to Backbone and though I know the general idea of how
to use it, my learning has been rapid and I'm probably missing some key
elements.
So I have a collection that contains an attribute called "type" which can
be article, book, video, class. I have the view rendering and everything
but I need to be able to filter the collection when links are clicked.
My question is - how can I get it to filter down the collection and still
be able to refilter the original collection when I click on another type?
Here's the gist of my code, I simplified it for easy reading:
var TagsView = Backbone.View.extend({
initialize: function(query) {
this.collection = new TagsCollection([], {query: self.apiQuery} );
this.collection.on('sync', function() {
self.render();
});
this.collection.on('reset', this.render, this);
},
render: function() {
//renders the template just fine
},
filter: function() {
//filtered does work correctly the first time I click on it but
not the second.
var filtered = this.collection.where({'type':filter});
this.collection.reset(filtered);
}
});

No comments:

Post a Comment