I am using Backbone and decided that I wanted a way to differentiate between HTML elements that were bound and those that were not.
So I would write (in HAML):
.container
.title(name='title')
.separator
As you can see it's clear that the dynamic element is title.
The reason for this was so I could mess around with the style and rename classes without worrying about breaking the app. It also means in the template I can tell what the dynamic elements are without needing to go back and forth with the Backbone View.
This means that I use $('[name=title]', this.el)
to reference this element from code. I am wondering if this is slow and be a noticeable issue if used everywhere. I have read that id
is fastest. I am using lists of items so id
is unrealistic. How does class
compare to name
lookups?
Also, if you have suggestions about keeping track of dynamic elements in HTML templates I'd love to hear them.
FYI:
I got the idea because I was originally using the Backbone.ModelBinding plugin which used
data-bind
attributes for dynamic elements, but I am moving away from it now.I'm using CoffeeScript, Backbone and haml_coffee templates.
I've also read the
$(this.el).find('[name=title]')
is faster than providing context to the selector.
Follow-up question:
A convention for indicating whether an HTML element is referenced from JS code
Updated jsperf to test all suggestions: