3 ms·
How is the performance when each row gets its own object instantiated, its own template loaded, and its own events attached? What happens when you have 1000+ ro
by chime 14y ago
How is the performance when each row gets its own object instantiated, its own template loaded, and its own events attached? What happens when you have 1000+ rows in a scrollable/sortable/filterable table? I tried nested views approach but the performance was lacking in slower PCs when the row count was high.
What I prefer to do is the following:
class Example.Views.TableView extends Backbone.View
events:
'click td': 'clicked'
rowHtml: ->
x = []
p = $('#person_row').html()
for person in @collection.models
x.push(_.template(p, person))
x.join('')
clicked: (e) ->
id = $(e.target).parents('tr').attr('id')
# handle action for row id
render: ->
# render self view including rowHTML
The template is loaded just once into a variable and filled in each iteration. Only one set of events is attached to the DOM. Only one call is made to DOM to create all the rows, one string merged from HTML array. I still get the benefit of view and subview templates, without the constructor for subview objects being called 1000 times when all I need is the <tr> HTML x 1000.
- jashkenas 14y agoYep -- when you're dealing with large numbers of DOM elements, you absolutely don't want to have an individual View for each element. At the other extreme, you don't want to have a single View for your entire application. Find a balance that makes sense, and represents a logical chunk of UI. For a bit more, see the FAQ, particularly the bottom of: http://backbonejs.org/#FAQ-tim-toady http://backbonejs.org/#FAQ-tim-toady
- fooandbarify 14y agoIs there a good rule of thumb as to what constitutes a "large number" of Views? I tend to assign Views to Models rather than Collections, partly because I hate storing Model ids as DOM attributes and extracting them in event handlers (as in the grandparent comment). I've gotten away with several hundred Views on a page before, but that doesn't feel quite right either.