I'm new to Backbone and have been testing different ways to filter and render a collection. I'm building a simple todo app. My model looks like this:
{ description: "go for a hike", display: "show" }
I want to use a search-box to filter my todos based on their "description" property.
As of now, on every 'keyup' I iterate through my collection and see if each model's description includes the value of the search-box. If the model does not include the value of the search-box then I change it's "display" property to "hide", else I keep/change it to "show".
After I've changed the 'display' property on every model instance I then render my collection. The collectionView only renders models whose 'display' property === 'show'
It turn out that this is pretty slow. Can anybody suggest a better way of filtering and rendering a collection on every 'keyup' ?
Here is the filter code:
filterResults: function() {
// value respresents the value of the search-box
var value = this.$el.val();
todoList.forEach( function (item) {
if (item.get('description').indexOf(value) > -1) {
item.set({display: 'show'});
}else {
item.set({display: "hide"});
}
});
todoListView.render();
}
Here is the the CollectionView code:
TodoListView = Backbone.View.extend({
addOne: function(todoItem) {
if (todoItem.get('display') === "show") {
var todoView = new TodoView({model: todoItem});
todoView.render();
this.$el.append(todoView.el);
}
},
render: function() {
this.$el.html("");
this.collection.forEach(this.addOne, this);
},
I appreciate the feedback, thanks!