javascript - Filtering and Rendering in Backbone -
i'm new backbone , have been testing different ways filter , render collection. i'm building simple todo app. model looks this:
{ description: "go hike", display: "show" } i want use search-box filter todos based on "description" property.
as of now, on every 'keyup' iterate through collection , see if each model's description includes value of search-box. if model not include value of search-box change it's "display" property "hide", else keep/change "show".
after i've changed 'display' property on every model instance render collection. collectionview renders models 'display' property === 'show'
it turn out pretty slow. can suggest better way of filtering , rendering collection on every 'keyup' ?
here filter code:
filterresults: function() { // value respresents value of 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 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 feedback, thanks!
you have 2 foreach blocs, depending of size of collection, can little slow...
can call render() when perform set on model? way avoid 1 foreach. maybe change how check if model have 'show' property, if you're focused on performance, try compare int instead of string.
i advise use http://www.datatables.net/, it's avery plugin , you.
hope helps.
Comments
Post a Comment