Features
- Adds structure to Javascript Code. (M-V-*)
- Extensible and customizable.
- Allows querying and manipulating data.
- Provides just enough data-structures & views for dynamic apps.
Pros
- Mature Framework
- Scales well.
- Can be embedded with other libraries.
- Lots of plug-ins and extensions authored.
- Works with PhoneGap
When to use Backbone?
- Single page applications (SPA's)
- Require structure for your code (MV* per say).
- Require data binding.
- Good for medium & complex applications.
Underscore.JS
- Provides functions commonly used in javascript.
- Provides most widely used data strucures.
- Companion templating plug-in.
- Its a dependency for Backbone.js.
When to use Underscore?
- Single page applications (SPA's)
- When complex operations on data structures are frequent.
- To allow templating with backbone.
Samples
<script id="item-template" type="text/template">
<div>
<input type="checkbox" id="todo_complete" <%= completed?'checked="checked"' : ''%>>
<%- title %>
</div>
</script>
var TodoView = Backbone.View.extend( {
tagName: "li";,
todoTpl: _.template("item-template");,
events: {
'dblclick label':'edit',
'keypress .edit':'updateOnEnter',
'blur .edit':'close'
},
render: function() {
this.$el.html(this.todoTpl(this.model.toJSON()));
this.input = this.$('.edit');
return this;
}
});