DailyJS

backbone.js: JavaScript MVC

Ric Roberts

Subscribe

@dailyjs

Facebook

Google+

libraries data underscore

backbone.js: JavaScript MVC

Posted by Ric Roberts on .
Featured

libraries data underscore

backbone.js: JavaScript MVC

Posted by Ric Roberts on .

backbone.js from DocumentCloud is a small (2KB, packed) open-source library which allows you to apply the MVC
(Model-View-Controller) principle to your JavaScript applications. It helps provide a bit of structure for rich client side apps, by
preventing you from having to tie data directly to the DOM. When models
change, the views representing the data can be notified, causing them to
automatically re-render.

To define a model, you extend Backbone.Model with your own
properties and methods, and the framework provides ways to get and set
the attributes, set up validations, serialize the object, detect if it
has changed, etc. To save a valid model to the persistence layer,
Backbone will delegate to its sync function and make a
relevant RESTful request (using JSON by default) to the server.

Backbone proxies to DocumentCloud's
underscore library for dealing with collections of models.

For views, create a custom view class which extends
Backbone.view, and then instantiate it, passing some
options which can include the model, a collection, the HTML element,
etc... You can define a render function on the view to
setup the view (with whatever flavour of templating or DOM manipulation
you like), and this can be bound to model events.

The documentation is well
written and comprehensive, providing good examples, and there's a
test-suite too. Get the code on
Github