I’m loading JSON data (array of objects) via service, onReady, and want to display this data in a grid. I have each row represented by a view.
render: function(){
var self = this;
(self.collection.models).forEach( function(model){
var rowView = new RowView({model: model});
self.$el.append(rowView.render().el);
});
}
Is it possible to build subviews and push them all at once to the DOM instead of going 1 by 1? Does the browser reflow & repaint happen on every append?
Ive seen all the ways people add subviews/children, but none of them solve the problem (frequent DOM access?) because this is just how backbone is built?
Yeah, that can be done. Generate an html element with jQuery (using the view’s
tagNamedefinition andattributesand all that) and then append everything to that. When you’re done, swap out the currentthis.$elwith the new one:That should do it.
Of course, you’re going to see a bit of a flicker on the screen, depending on how fast the browser is and how large the change is. But this should get you down the road to do what you want.
More info about replaceWith: http://api.jquery.com/replaceWith/