Re:Hello Backbone.js - A step-by-step tutorial
by shaunxcode on Tuesday June 07, 2011 @11:58PM
(#2631968)
I have a class called Tag with static methods for all relevant tag names which merely wraps $('<' + tagname + '/>') and thus allow for stuff like:
Tag.ul(
Tag.li('unclickable li tag')
.addClass('unclickable'),
Tag.li('test clickable li tag')
.addClass('clickable')
.click(function() {
alert('clicked on test')}))
I prefer this as it keeps my view code "in language" and makes it easy to apply any sort of jquery methods which I need to the different elements.
If Underscore's syntax isn't your cup of tea, there's a slew of other libraries available, like Mustache or jQuery's own tmpl module. You'll never go back to building the DOM yourself or dozens of concatenated strings of html again!