backbone.js - Make backbone wait to render view -
i'm new backbone , have collection of objects , view displays them list. set every time object added collection view re-rendered. inefficient because if add 20 things collection @ once, rendered 20 times when needs rendered 1 time after last item has been added. how make backbone hold off on rendering until i'm done adding things collection?
here code:
<html> <head> <meta charset="utf-8" /> <title>looking @ underscore.js templates</title> </head> <body> <script type="text/template" class="template"> <ul id="petlist"> <% _.each(model.pets, function(pet) { %> <li><%- pet.name %> (<%- pet.type %>)</li> <% }); %> </ul> </script> <div id="container"/> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="http://underscorejs.org/underscore.js"></script> <script src="http://backbonejs.org/backbone-min.js"></script> <script type="text/javascript"> var servicedata = [ { name: "peaches", type: "dog" }, { name: "wellington", type: "cat" }, { name: "beefy", type: "dog" } ]; $(document).ready(function() { var pet = backbone.model.extend({ name: null, type: null }); var pets = backbone.collection.extend(); var appview = backbone.view.extend({ updateui: function(model) { _.templatesettings.variable = "model"; var template = _.template($("script.template").html()); var model = { pets: model.collection.tojson() }; var html = template(model); $("#container").html(html); } }); var pets = new pets(); var av = new appview(); pets.bind("add", av.updateui); pets.set(servicedata); }); </script> </body> </html>
you need re-factor couple of things on code. specific case need use reset instead of set, dispatch 1 event when data set. pass collection view , view listen reset event.
additionally, prevent stress of browser use document.createdocumentfragment dom holder speed treatment of append. check point number 2 more reference: http://ozkatz.github.io/avoiding-common-backbonejs-pitfalls.html
<html> <head> <meta charset="utf-8" /> <title>looking @ underscore.js templates</title> </head> <body> <script type="text/template" class="template"> <ul id="petlist"> <% _.each(model.pets, function(pet) { %> <li><%- pet.name %> (<%- pet.type %>)</li> <% }); %> </ul> </script> <div id="container"/> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="http://underscorejs.org/underscore.js"></script> <script src="http://backbonejs.org/backbone-min.js"></script> <script type="text/javascript"> var servicedata = [ { name: "peaches", type: "dog" }, { name: "wellington", type: "cat" }, { name: "beefy", type: "dog" } ]; $(document).ready(function() { var pet = backbone.model.extend({ name: null, type: null }); var pets = backbone.collection.extend(); var appview = backbone.view.extend({ initialize : function(){ this.collection.bind("reset", av.updateui); }, updateui: function() { $items = document.createdocumentfragment(); this.collection.each(function(model){ var itemview = new itemview({model : model}); $items.append(itemview.el); itemview.render(); }); $("#container").html($items); } }); var pets = new pets(); var av = new appview({collection : pets}); pets.reset(servicedata); }); </script> </body> </html>
Comments
Post a Comment