Backbone.js 文档摘要

Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

The project is hosted on Github, and the annotated source code is available, as well as online test suite, an example application, a list of tutorials and a long list of real-world projects that use Backbone. Backbone is available for the use under the MIT software license.


Backbone’s only hard dependency is Underscore.js (>= 1.5.0). For RESTful persistence, history support via Backbone.Router and DOM manipulation with Backbone.View, include jQuery, and json2.js for older Internet Explorer support. (Mimics of the Underscore and jQuery APIs, such as Lo-Dash and Zepto, will also tend to work, with varying degrees of compatibility.)


When working on a web application that involves a lot of Javascript, one of the first things you learn is to stop tying your data to the DOM. It’s all too easy to create Javascript applications that end up as tangled piles of jQuery selectors and callbacks, all trying frantically to keep data in sync between the HTML UI, your Javascript logic, and the database on your server. For rich client-side applications, a more structured approach is often helpful.

With Backbone, you represent your data as Models, which can be created, validated, destroyed, and saved to the server. Whenever a UI action causes an attribute of a model to change, the model triggers a “change” event; all the Views that display the model’s state can be notified of the change, so that they are able to response accordingly, re-rendering themselves with the new information. In a finished Backbone app, you don’t have to write the glue code that looks into the DOM to find an element with a specific id, and update the HTML manually – when the model changes, the views simply update themselves.

Philosophically, Backbone is an attempt to discover the minimal set of data-structuring (models and collections) and user interface (views and URLs) primitives that are generally useful when building web applications with JavaScript. In an ecosystem where overarching, decides-everything-for-you frameworks are commonplace, and many libraries require your site to be reorganized to suit their look, feel, and default behavior – Backbone should continue to be a tool that gives you the freedom to design the full experience of your web application.


Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments. For example:

var object = {};

_.extend(object, Backbone.Events);

object.on('alert', function(msg) {
    alert('Triggered ' + msg);

object.trigger('alert', 'an event');

For example, to make a handy event dispatcher that can coordinate events among different areas of your application: var dispatcher = _.clone(Backbone.Events)


object.on(event, callback, [context]) Alias: bind

Bind a callback function to an object. The callback will be invoked whenever the event is fired. If you have a large number of different events on a page, the convention is to use colons to namespace them: "poll:start", or "change:selection". The event string may also be a space-delimited list of several events…

book.on('change:title change:author', ...);

To supply a context value for this when the callback is invoked, pass the optional third argument: model.on('change', this.render, this)

Callbacks bound to the special "all" event will be triggered when any event occurs, and are passed the name of the event as the first argument. For example, to proxy all events from one object to another:

proxy.on('all', function() {

All Backbone event methods also support an event map syntax, as an alternative to positional arguments:

    "change:title": titleView.update,
    "change:author": authorPane.update,
    "destroy": bookView.remove

off[event], [callback], [context]) Alias: unbind

Remove a previously-bound callback function from an object. If no context is specified, all of the versions of the callback with different contexts will be removed. If no callback is specified, all callbacks for the event will be removed. If no event is specified, callbacks for all events will be removed.

// Removes just the `onChange` callback."change", onChange);

// Removes all "change" callbacks."change");

// Removes the `onChange` callback for all events., onChange);

// Removes all callbacks for `context` for all events., null, context);

// Removes all callbacks on `object`.;

Note that calling, for example, will indeed remove all events on the model – incliding events that Backbone uses for internal bookkeeping.


object.trigger(event, [* args])

Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.


object.once(event, callback, [context])

Just like on, but causes the bound callback to only fire once before being removed. Handy for saying “the next time that X happens, do this”.



您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>