Permalink
5119 lines (4513 sloc) 209 KB
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="http://backbonejs.org" />
<title>Backbone.js</title>
<style>
body {
font-size: 14px;
line-height: 22px;
font-family: Helvetica Neue, Helvetica, Arial;
background: #f4f4f4 url(docs/images/background.png);
}
.interface {
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
}
div#sidebar {
background: #fff;
position: fixed;
z-index: 10;
top: 0; left: 0; bottom: 0;
width: 200px;
overflow-y: auto;
overflow-x: hidden;
-webkit-overflow-scrolling: touch;
padding: 15px 0 30px 30px;
border-right: 1px solid #bbb;
box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
}
a.toc_title, a.toc_title:visited {
display: block;
color: black;
font-weight: bold;
margin-top: 15px;
}
a.toc_title:hover {
text-decoration: underline;
}
#sidebar .version {
font-size: 10px;
font-weight: normal;
}
ul.toc_section {
font-size: 11px;
line-height: 14px;
margin: 5px 0 0 0;
padding-left: 0px;
list-style-type: none;
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif;
}
.toc_section li {
cursor: pointer;
margin: 0 0 3px 0;
}
.toc_section li a {
text-decoration: none;
color: black;
}
.toc_section li a:hover {
text-decoration: underline;
}
input#function_filter {
width: 80%;
}
div.container {
position: relative;
width: 550px;
margin: 40px 0 50px 260px;
}
img#logo {
width: 450px;
height: 80px;
}
div.run {
position: absolute;
right: 15px;
width: 26px; height: 18px;
background: url('docs/images/arrows.png') no-repeat -26px 0;
}
div.run:active {
background-position: -51px 0;
}
p, div.container ul {
margin: 25px 0;
width: 550px;
}
p.warning {
font-size: 12px;
line-height: 18px;
font-style: italic;
}
div.container ul {
list-style: circle;
padding-left: 15px;
font-size: 13px;
line-height: 18px;
}
div.container ul li {
margin-bottom: 10px;
}
div.container ul.small {
font-size: 12px;
}
a, a:visited {
color: #444;
}
a:active, a:hover {
color: #000;
}
a.punch {
display: inline-block;
background: #4162a8;
border-top: 1px solid #38538c;
border-right: 1px solid #1f2d4d;
border-bottom: 1px solid #151e33;
border-left: 1px solid #1f2d4d;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
-ms-border-radius: 4px;
-o-border-radius: 4px;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
color: #fff;
font: bold 14px "helvetica neue", helvetica, arial, sans-serif;
line-height: 1;
margin-bottom: 15px;
padding: 8px 0 10px 0;
text-align: center;
text-shadow: 0px -1px 1px #1e2d4d;
text-decoration: none;
width: 225px;
-webkit-background-clip: padding-box; }
a.punch:hover {
-webkit-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-moz-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-ms-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
-o-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
cursor: pointer; }
a.punch:active {
-webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
-moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
-ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
-o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
margin-top: 5px; margin-bottom: 10px; }
a img {
border: 0;
}
a.travis-badge {
display: block;
}
h1, h2, h3, h4, h5, h6 {
padding-top: 20px;
}
h2 {
font-size: 22px;
}
b.header {
font-size: 18px;
line-height: 35px;
}
span.alias {
font-size: 14px;
font-style: italic;
margin-left: 20px;
}
table {
margin: 15px 0 0; padding: 0;
}
tr, td {
margin: 0; padding: 0;
}
td {
padding: 0px 15px 5px 0;
}
table .rule {
height: 1px;
background: #ccc;
margin: 5px 0;
}
code, pre, tt {
font-family: Monaco, Consolas, "Lucida Console", monospace;
font-size: 12px;
line-height: 18px;
font-style: normal;
}
tt {
padding: 0px 3px;
background: #fff;
border: 1px solid #ddd;
zoom: 1;
}
code {
margin-left: 20px;
}
pre {
font-size: 12px;
padding: 2px 0 2px 15px;
border: 4px solid #bbb; border-top: 0; border-bottom: 0;
margin: 0px 0 25px;
}
img.example_image {
margin: 0px auto;
}
img.example_retina {
margin: 20px;
box-shadow: 0 8px 15px rgba(0,0,0,0.4);
}
@media only screen and (-webkit-max-device-pixel-ratio: 1) and (max-width: 600px),
only screen and (max--moz-device-pixel-ratio: 1) and (max-width: 600px) {
div#sidebar {
display: none;
}
img#logo {
max-width: 450px;
width: 100%;
height: auto;
}
div.container {
width: auto;
margin-left: 15px;
margin-right: 15px;
}
p, div.container ul {
width: auto;
}
}
@media only screen and (-webkit-min-device-pixel-ratio: 1.5) and (max-width: 640px),
only screen and (-o-min-device-pixel-ratio: 3/2) and (max-width: 640px),
only screen and (min-device-pixel-ratio: 1.5) and (max-width: 640px) {
img {
max-width: 100%;
height: auto;
}
div#sidebar {
-webkit-overflow-scrolling: initial;
position: relative;
width: 90%;
height: 120px;
left: 0;
top: -7px;
padding: 10px 0 10px 30px;
border: 0;
}
img#logo {
width: auto;
height: auto;
}
div.container {
margin: 0;
width: 100%;
}
p, div.container ul {
max-width: 98%;
overflow-x: scroll;
}
table {
position: relative;
}
tr:first-child td {
padding-bottom: 25px;
}
td.text {
line-height: 12px;
padding: 0;
position: absolute;
left: 0;
top: 48px;
}
tr:last-child td.text {
top: 122px;
}
pre {
overflow: scroll;
}
}
img.figure {
width: 100%;
}
div.columns {
display: table;
table-layout: fixed;
width: 100%;
}
div.columns ul {
margin: 10px 0;
}
div.col-50 {
display: table-cell;
width: 50%;
}
</style>
</head>
<body>
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
Backbone.js <span class="version">(1.3.3)</span>
</a>
<ul class="toc_section">
<li>&raquo; <a href="http://github.com/jashkenas/backbone">GitHub Repository</a></li>
<li>&raquo; <a href="docs/backbone.html">Annotated Source</a></li>
</ul>
<input id="function_filter" placeholder="Filter" type="text" autofocus />
<div class="searchable_section">
<a class="toc_title" href="#Getting-started">
Getting Started
</a>
<ul class="toc_section">
<li data-name="Introduction">- <a href="#Getting-started">Introduction</a></li>
<li data-name="Models and Views">– <a href="#Model-View-separation">Models and Views</a></li>
<li data-name="Collections">– <a href="#Model-Collections">Collections</a></li>
<li data-name="API Integration">– <a href="#API-integration">API Integration</a></li>
<li data-name="Rendering">– <a href="#View-rendering">Rendering</a></li>
<li data-name="Routing">– <a href="#Routing">Routing</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Events">
Events
</a>
<ul class="toc_section">
<li data-name="on">– <a href="#Events-on">on</a></li>
<li data-name="off">– <a href="#Events-off">off</a></li>
<li data-name="trigger">– <a href="#Events-trigger">trigger</a></li>
<li data-name="once">– <a href="#Events-once">once</a></li>
<li data-name="listenTo">– <a href="#Events-listenTo">listenTo</a></li>
<li data-name="stopListening">– <a href="#Events-stopListening">stopListening</a></li>
<li data-name="listenToOnce">– <a href="#Events-listenToOnce">listenToOnce</a></li>
<li data-name="Catalog of Built-in Events">- <a href="#Events-catalog"><b>Catalog of Built-in Events</b></a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Model">
Model
</a>
<ul class="toc_section">
<li data-name="extend">– <a href="#Model-extend">extend</a></li>
<li data-name="constructor / initialize">– <a href="#Model-constructor">constructor / initialize</a></li>
<li data-name="get">– <a href="#Model-get">get</a></li>
<li data-name="set">– <a href="#Model-set">set</a></li>
<li data-name="escape">– <a href="#Model-escape">escape</a></li>
<li data-name="has">– <a href="#Model-has">has</a></li>
<li data-name="unset">– <a href="#Model-unset">unset</a></li>
<li data-name="clear">– <a href="#Model-clear">clear</a></li>
<li data-name="id">– <a href="#Model-id">id</a></li>
<li data-name="idAttribute">– <a href="#Model-idAttribute">idAttribute</a></li>
<li data-name="cid">– <a href="#Model-cid">cid</a></li>
<li data-name="attributes">– <a href="#Model-attributes">attributes</a></li>
<li data-name="changed">– <a href="#Model-changed">changed</a></li>
<li data-name="defaults">– <a href="#Model-defaults">defaults</a></li>
<li data-name="toJSON">– <a href="#Model-toJSON">toJSON</a></li>
<li data-name="sync">– <a href="#Model-sync">sync</a></li>
<li data-name="fetch">– <a href="#Model-fetch">fetch</a></li>
<li data-name="save">– <a href="#Model-save">save</a></li>
<li data-name="destroy">– <a href="#Model-destroy">destroy</a></li>
<li data-name="Underscore Methods">– <a href="#Model-Underscore-Methods"><b>Underscore Methods (9)</b></a></li>
<li data-name="validate">– <a href="#Model-validate">validate</a></li>
<li data-name="validationError">– <a href="#Model-validationError">validationError</a></li>
<li data-name="isValid">– <a href="#Model-isValid">isValid</a></li>
<li data-name="url">– <a href="#Model-url">url</a></li>
<li data-name="urlRoot">– <a href="#Model-urlRoot">urlRoot</a></li>
<li data-name="parse">– <a href="#Model-parse">parse</a></li>
<li data-name="clone">– <a href="#Model-clone">clone</a></li>
<li data-name="isNew">– <a href="#Model-isNew">isNew</a></li>
<li data-name="hasChanged">– <a href="#Model-hasChanged">hasChanged</a></li>
<li data-name="changedAttributes">– <a href="#Model-changedAttributes">changedAttributes</a></li>
<li data-name="previous">– <a href="#Model-previous">previous</a></li>
<li data-name="previousAttributes">– <a href="#Model-previousAttributes">previousAttributes</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Collection">
Collection
</a>
<ul class="toc_section">
<li data-name="extend">– <a href="#Collection-extend">extend</a></li>
<li data-name="model">– <a href="#Collection-model">model</a></li>
<li data-name="modelId">– <a href="#Collection-modelId">modelId</a></li>
<li data-name="constructor / initialize" data-name="constructor / initialize">– <a href="#Collection-constructor">constructor / initialize</a></li>
<li data-name="models">– <a href="#Collection-models">models</a></li>
<li data-name="toJSON">– <a href="#Collection-toJSON">toJSON</a></li>
<li data-name="sync">– <a href="#Collection-sync">sync</a></li>
<li data-name="Underscore Methods">– <a href="#Collection-Underscore-Methods"><b>Underscore Methods (46)</b></a></li>
<li data-name="add">– <a href="#Collection-add">add</a></li>
<li data-name="remove">– <a href="#Collection-remove">remove</a></li>
<li data-name="reset">– <a href="#Collection-reset">reset</a></li>
<li data-name="set">– <a href="#Collection-set">set</a></li>
<li data-name="get">– <a href="#Collection-get">get</a></li>
<li data-name="at">– <a href="#Collection-at">at</a></li>
<li data-name="push">– <a href="#Collection-push">push</a></li>
<li data-name="pop">– <a href="#Collection-pop">pop</a></li>
<li data-name="unshift">– <a href="#Collection-unshift">unshift</a></li>
<li data-name="shift">– <a href="#Collection-shift">shift</a></li>
<li data-name="slice">– <a href="#Collection-slice">slice</a></li>
<li data-name="length">– <a href="#Collection-length">length</a></li>
<li data-name="comparator">– <a href="#Collection-comparator">comparator</a></li>
<li data-name="sort">– <a href="#Collection-sort">sort</a></li>
<li data-name="pluck">– <a href="#Collection-pluck">pluck</a></li>
<li data-name="where">– <a href="#Collection-where">where</a></li>
<li data-name="findWhere">– <a href="#Collection-findWhere">findWhere</a></li>
<li data-name="url">– <a href="#Collection-url">url</a></li>
<li data-name="parse">– <a href="#Collection-parse">parse</a></li>
<li data-name="clone">– <a href="#Collection-clone">clone</a></li>
<li data-name="fetch">– <a href="#Collection-fetch">fetch</a></li>
<li data-name="create">– <a href="#Collection-create">create</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Router">
Router
</a>
<ul class="toc_section">
<li data-name="extend">– <a href="#Router-extend">extend</a></li>
<li data-name="routes">– <a href="#Router-routes">routes</a></li>
<li data-name="constructor / initialize">– <a href="#Router-constructor">constructor / initialize</a></li>
<li data-name="route">– <a href="#Router-route">route</a></li>
<li data-name="navigate">– <a href="#Router-navigate">navigate</a></li>
<li data-name="execute">– <a href="#Router-execute">execute</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#History">
History
</a>
<ul class="toc_section">
<li data-name="start">– <a href="#History-start">start</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Sync">
Sync
</a>
<ul class="toc_section">
<li data-name="Backbone.sync">– <a href="#Sync">Backbone.sync</a></li>
<li data-name="Backbone.ajax">– <a href="#Sync-ajax">Backbone.ajax</a></li>
<li data-name="Backbone.emulateHTTP">– <a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
<li data-name="Backbone.emulateJSON">– <a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#View">
View
</a>
<ul class="toc_section">
<li data-name="extend">– <a href="#View-extend">extend</a></li>
<li data-name="constructor / initialize">– <a href="#View-constructor">constructor / initialize</a></li>
<li data-name="el">– <a href="#View-el">el</a></li>
<li data-name="$el">– <a href="#View-$el">$el</a></li>
<li data-name="setElement">– <a href="#View-setElement">setElement</a></li>
<li data-name="attributes">– <a href="#View-attributes">attributes</a></li>
<li data-name="$ (jQuery)">– <a href="#View-dollar">$ (jQuery)</a></li>
<li data-name="template">– <a href="#View-template">template</a></li>
<li data-name="render">– <a href="#View-render">render</a></li>
<li data-name="remove">– <a href="#View-remove">remove</a></li>
<li data-name="events">– <a href="#View-events">events</a></li>
<li data-name="delegateEvents">– <a href="#View-delegateEvents">delegateEvents</a></li>
<li data-name="undelegateEvents">– <a href="#View-undelegateEvents">undelegateEvents</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#Utility">
Utility
</a>
<ul class="toc_section">
<li data-name="Backbone.noConflict">– <a href="#Utility-Backbone-noConflict">Backbone.noConflict</a></li>
<li data-name="Backbone.$">– <a href="#Utility-Backbone-$">Backbone.$</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#faq">
F.A.Q.
</a>
<ul class="toc_section">
<li data-name="Why Backbone?">– <a href="#FAQ-why-backbone">Why Backbone?</a></li>
<li data-name="More Than One Way To Do It">– <a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
<li data-name="Nested Models and Collections">– <a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
<li data-name="Loading Bootstrapped Models">– <a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
<li data-name="Extending Backbone">– <a href="#FAQ-extending">Extending Backbone</a></li>
<li data-name="Traditional MVC">– <a href="#FAQ-mvc">Traditional MVC</a></li>
<li data-name="Binding this">– <a href="#FAQ-this">Binding "this"</a></li>
<li data-name="Working with Rails">– <a href="#FAQ-rails">Working with Rails</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#examples">
Examples
</a>
<ul class="toc_section">
<li data-name="Todos">– <a href="#examples-todos">Todos</a></li>
<li data-name="DocumentCloud">– <a href="#examples-documentcloud">DocumentCloud</a></li>
<li data-name="USA Today">– <a href="#examples-usa-today">USA Today</a></li>
<li data-name="Rdio">– <a href="#examples-rdio">Rdio</a></li>
<li data-name="Hulu">– <a href="#examples-hulu">Hulu</a></li>
<li data-name="Quartz">– <a href="#examples-quartz">Quartz</a></li>
<li data-name="Earth">– <a href="#examples-earth">Earth</a></li>
<li data-name="Vox">– <a href="#examples-vox">Vox</a></li>
<li data-name="Gawker Media">– <a href="#examples-gawker">Gawker Media</a></li>
<li data-name="Flow">– <a href="#examples-flow">Flow</a></li>
<li data-name="Gilt Groupe">– <a href="#examples-gilt">Gilt Groupe</a></li>
<li data-name="Enigma">– <a href="#examples-enigma">Enigma</a></li>
<li data-name="NewsBlur">– <a href="#examples-newsblur">NewsBlur</a></li>
<li data-name="WordPress.com">– <a href="#examples-wordpress">WordPress.com</a></li>
<li data-name="Foursquare">– <a href="#examples-foursquare">Foursquare</a></li>
<li data-name="Bitbucket">– <a href="#examples-bitbucket">Bitbucket</a></li>
<li data-name="Disqus">– <a href="#examples-disqus">Disqus</a></li>
<li data-name="Delicious">– <a href="#examples-delicious">Delicious</a></li>
<li data-name="Khan Academy">– <a href="#examples-khan-academy">Khan Academy</a></li>
<li data-name="IRCCloud">– <a href="#examples-irccloud">IRCCloud</a></li>
<li data-name="Pitchfork">– <a href="#examples-pitchfork">Pitchfork</a></li>
<li data-name="Spin">– <a href="#examples-spin">Spin</a></li>
<li data-name="ZocDoc">– <a href="#examples-zocdoc">ZocDoc</a></li>
<li data-name="Walmart Mobile">– <a href="#examples-walmart">Walmart Mobile</a></li>
<li data-name="Groupon Now!">– <a href="#examples-groupon">Groupon Now!</a></li>
<li data-name="Basecamp">– <a href="#examples-basecamp">Basecamp</a></li>
<li data-name="Slavery Footprint">– <a href="#examples-slavery-footprint">Slavery Footprint</a></li>
<li data-name="Stripe">– <a href="#examples-stripe">Stripe</a></li>
<li data-name="Airbnb">– <a href="#examples-airbnb">Airbnb</a></li>
<li data-name="SoundCloud Mobile">– <a href="#examples-soundcloud">SoundCloud Mobile</a></li>
<li data-name="Art.sy">- <a href="#examples-artsy">Art.sy</a></li>
<li data-name="Pandora">– <a href="#examples-pandora">Pandora</a></li>
<li data-name="Inkling">– <a href="#examples-inkling">Inkling</a></li>
<li data-name="Code School">– <a href="#examples-code-school">Code School</a></li>
<li data-name="CloudApp">– <a href="#examples-cloudapp">CloudApp</a></li>
<li data-name="SeatGeek">– <a href="#examples-seatgeek">SeatGeek</a></li>
<li data-name="Easel">– <a href="#examples-easel">Easel</a></li>
<li data-name="Jolicloud">- <a href="#examples-jolicloud">Jolicloud</a></li>
<li data-name="Salon.io">– <a href="#examples-salon">Salon.io</a></li>
<li data-name="TileMill">– <a href="#examples-tilemill">TileMill</a></li>
<li data-name="Blossom">– <a href="#examples-blossom">Blossom</a></li>
<li data-name="Trello">– <a href="#examples-trello">Trello</a></li>
<li data-name="Tzigla">– <a href="#examples-tzigla">Tzigla</a></li>
</ul>
</div>
<div class="searchable_section">
<a class="toc_title" href="#changelog">
Change Log
</a>
</div>
</div>
<div class="container">
<p>
<img id="logo" src="docs/images/backbone.png" alt="Backbone.js" />
</p>
<p>
Backbone.js gives structure to web applications
by providing <b>models</b> with key-value binding and custom events,
<b>collections</b> with a rich API of enumerable functions,
<b>views</b> with declarative event handling, and connects it all to your
existing API over a RESTful JSON interface.
</p>
<p>
The project is <a href="http://github.com/jashkenas/backbone/">hosted on GitHub</a>,
and the <a href="docs/backbone.html">annotated source code</a> is available,
as well as an online <a href="test/">test suite</a>,
an <a href="examples/todos/index.html">example application</a>,
a <a href="https://github.com/jashkenas/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>
and a <a href="#examples">long list of real-world projects</a> that use Backbone.
Backbone is available for use under the <a href="http://github.com/jashkenas/backbone/blob/master/LICENSE">MIT software license</a>.
</p>
<p>
You can report bugs and discuss features on the
<a href="http://github.com/jashkenas/backbone/issues">GitHub issues page</a>,
on Freenode IRC in the <tt>#documentcloud</tt> channel, post questions to the
<a href="https://groups.google.com/forum/#!forum/backbonejs">Google Group</a>,
add pages to the <a href="https://github.com/jashkenas/backbone/wiki">wiki</a>
or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
</p>
<p>
<i>
Backbone is an open-source component of
<a href="http://documentcloud.org/">DocumentCloud</a>.
</i>
</p>
<h2 id="downloads">
Downloads &amp; Dependencies
<span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
</h2>
<table>
<tr>
<td><a class="punch" href="backbone.js">Development Version (1.3.3)</a></td>
<td class="text"><i>72kb, Full source, tons of comments</i></td>
</tr>
<tr>
<td><a class="punch" href="backbone-min.js">Production Version (1.3.3)</a></td>
<td class="text" style="line-height: 16px;">
<i>7.6kb, Packed and gzipped</i><br />
<small>(<a href="backbone-min.map">Source Map</a>)</small>
</td>
</tr>
<tr>
<td><a class="punch" href="https://raw.github.com/jashkenas/backbone/master/backbone.js">Edge Version (master)</a></td>
<td>
<i>Unreleased, use at your own risk</i>
<a class="travis-badge" href="https://travis-ci.org/jashkenas/backbone">
<img src="https://travis-ci.org/jashkenas/backbone.png" />
</a>
</td>
</tr>
</table>
<p>
Backbone's only hard dependency is
<a href="http://underscorejs.org/">Underscore.js</a> <small>( >= 1.8.3)</small>.
For RESTful persistence and DOM manipulation with <a href="#View">Backbone.View</a>,
include <b><a href="https://jquery.com/">jQuery</a></b> ( >= 1.11.0), and
<b><a href="https://github.com/douglascrockford/JSON-js">json2.js</a></b> for older
Internet Explorer support.
<i>(Mimics of the Underscore and jQuery APIs, such as
<a href="https://lodash.com/">Lodash</a> and
<a href="http://zeptojs.com/">Zepto</a>, will
also tend to work, with varying degrees of compatibility.)</i>
</p>
<h2 id="Getting-started">Getting Started</h2>
<p>
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.
</p>
<p>
With Backbone, you represent your data as
<a href="#Model">Models</a>, 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 <i>"change"</i> event; all
the <a href="#View">Views</a> that display the model's state can be notified of the
change, so that they are able to respond 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 <i>id</i>,
and update the HTML manually
&mdash; when the model changes, the views simply update themselves.
</p>
<p>
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 <i>freedom</i> to design the full
experience of your web application.
</p>
<p>
If you're new here, and aren't yet quite sure what Backbone is for, start by
browsing the <a href="#examples">list of Backbone-based projects</a>.
</p>
<p>
Many of the code examples in this documentation are runnable, because
Backbone is included on this page.
Click the <i>play</i> button to execute them.
</p>
<h2 id="Model-View-separation">Models and Views</h2>
<img class="figure" src="docs/images/intro-model-view.svg" alt="Model-View Separation.">
<p>
The single most important thing that Backbone can help you with is keeping
your business logic separate from your user interface. When the two are
entangled, change is hard; when logic doesn't depend on UI, your
interface becomes easier to work with.
</p>
<div class="columns">
<div class="col-50">
<b>Model</b>
<ul>
<li>Orchestrates data and business logic.</li>
<li>Loads and saves data from the server.</li>
<li>Emits events when data changes.</li>
</ul>
</div>
<div class="col-50">
<b>View</b>
<ul>
<li>Listens for changes and renders UI.</li>
<li>Handles user input and interactivity.</li>
<li>Sends captured input to the model.</li>
</ul>
</div>
</div>
<p>
A <b>Model</b> manages an internal table of data attributes, and
triggers <tt>"change"</tt> events when any of its data is modified.
Models handle syncing data with a persistence layer — usually a REST API
with a backing database. Design your models as the atomic reusable objects
containing all of the helpful functions for manipulating their particular
bit of data. Models should be able to be passed around throughout your app,
and used anywhere that bit of data is needed.
</p>
<p>
A <b>View</b> is an atomic chunk of user interface. It often renders the
data from a specific model, or number of models &mdash; but views can
also be data-less chunks of UI that stand alone.
Models should be generally unaware of views. Instead, views listen to
the model <tt>"change"</tt> events, and react or re-render themselves
appropriately.
</p>
<h2 id="Model-Collections">Collections</h2>
<img class="figure" src="docs/images/intro-collections.svg" alt="Model Collections.">
<p>
A <b>Collection</b> helps you deal with a group of related models, handling
the loading and saving of new models to the server and providing helper
functions for performing aggregations or computations against a list of models.
Aside from their own events, collections also proxy through all of the
events that occur to models within them, allowing you to listen in one place
for any change that might happen to any model in the collection.
</p>
<h2 id="API-integration">API Integration</h2>
<p>
Backbone is pre-configured to sync with a RESTful API. Simply create a
new Collection with the <tt>url</tt> of your resource endpoint:
</p>
<pre>
var Books = Backbone.Collection.extend({
url: '/books'
});
</pre>
<p>
The <b>Collection</b> and <b>Model</b> components together form a direct
mapping of REST resources using the following methods:
</p>
<pre>
GET /books/ .... collection.fetch();
POST /books/ .... collection.create();
GET /books/1 ... model.fetch();
PUT /books/1 ... model.save();
DEL /books/1 ... model.destroy();
</pre>
<p>
When fetching raw JSON data from an API, a <b>Collection</b> will
automatically populate itself with data formatted as an array, while
a <b>Model</b> will automatically populate itself with data formatted
as an object:
</p>
<pre>
[{"id": 1}] ..... populates a Collection with one model.
{"id": 1} ....... populates a Model with one attribute.
</pre>
<p>
However, it's fairly common to encounter APIs that return data in a
different format than what Backbone expects. For example, consider
fetching a <b>Collection</b> from an API that returns the real data
array wrapped in metadata:
</p>
<pre>
{
"page": 1,
"limit": 10,
"total": 2,
"books": [
{"id": 1, "title": "Pride and Prejudice"},
{"id": 4, "title": "The Great Gatsby"}
]
}
</pre>
<p>
In the above example data, a <b>Collection</b> should populate using the
<tt>"books"</tt> array rather than the root object structure. This
difference is easily reconciled using a <tt>parse</tt> method that
returns (or transforms) the desired portion of API data:
</p>
<pre>
var Books = Backbone.Collection.extend({
url: '/books',
parse: function(data) {
return data.books;
}
});
</pre>
<h2 id="View-rendering">View Rendering</h2>
<img class="figure" src="docs/images/intro-views.svg" alt="View rendering.">
<p>
Each <b>View</b> manages the rendering and user interaction within its own
DOM element. If you're strict about not allowing views to reach outside
of themselves, it helps keep your interface flexible &mdash; allowing
views to be rendered in isolation in any place where they might be needed.
</p>
<p>
Backbone remains unopinionated about the process used to render <b>View</b>
objects and their subviews into UI: you define how your models get translated
into HTML (or SVG, or Canvas, or something even more exotic).
It could be as prosaic as a simple
<a href="http://underscorejs.org/#template">Underscore template</a>, or as fancy as the
<a href="http://facebook.github.io/react/docs/tutorial.html">React virtual DOM</a>.
Some basic approaches to rendering views can be found
in the <a href="https://github.com/jashkenas/backbone/wiki/Backbone%2C-The-Primer">Backbone primer</a>.
</p>
<h2 id="Routing">Routing with URLs</h2>
<img class="figure" src="docs/images/intro-routing.svg" alt="Routing">
<p>
In rich web applications, we still want to provide linkable,
bookmarkable, and shareable URLs to meaningful locations within an app.
Use the <b>Router</b> to update the browser URL whenever the user
reaches a new "place" in your app that they might want to bookmark or share.
Conversely, the <b>Router</b> detects changes to the URL &mdash; say,
pressing the "Back" button &mdash; and can tell your application exactly where you
are now.
</p>
<h2 id="Events">Backbone.Events</h2>
<p>
<b>Events</b> 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:
</p>
<pre class="runnable">
var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
alert("Triggered " + msg);
});
object.trigger("alert", "an event");
</pre>
<p>
For example, to make a handy event dispatcher that can coordinate events
among different areas of your application: <tt>var dispatcher = _.clone(Backbone.Events)</tt>
</p>
<p id="Events-on">
<b class="header">on</b><code>object.on(event, callback, [context])</code><span class="alias">Alias: bind</span>
<br />
Bind a <b>callback</b> function to an object. The callback will be invoked
whenever the <b>event</b> is fired.
If you have a large number of different events on a page, the convention is to use colons to
namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>.
The event string may also be a space-delimited list of several events...
</p>
<pre>
book.on("change:title change:author", ...);
</pre>
<p>
Callbacks bound to the special
<tt>"all"</tt> 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:
</p>
<pre>
proxy.on("all", function(eventName) {
object.trigger(eventName);
});
</pre>
<p>
All Backbone event methods also support an event map syntax, as an alternative
to positional arguments:
</p>
<pre>
book.on({
"change:author": authorPane.update,
"change:title change:subtitle": titleView.update,
"destroy": bookView.remove
});
</pre>
<p>
To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
pass the optional last argument: <tt>model.on('change', this.render, this)</tt> or
<tt>model.on({change: this.render}, this)</tt>.
</p>
<p id="Events-off">
<b class="header">off</b><code>object.off([event], [callback], [context])</code><span class="alias">Alias: unbind</span>
<br />
Remove a previously-bound <b>callback</b> function from an object. If no
<b>context</b> is specified, all of the versions of the callback with
different contexts will be removed. If no
callback is specified, all callbacks for the <b>event</b> will be
removed. If no event is specified, callbacks for <i>all</i> events
will be removed.
</p>
<pre>
// Removes just the `onChange` callback.
object.off("change", onChange);
// Removes all "change" callbacks.
object.off("change");
// Removes the `onChange` callback for all events.
object.off(null, onChange);
// Removes all callbacks for `context` for all events.
object.off(null, null, context);
// Removes all callbacks on `object`.
object.off();
</pre>
<p>
Note that calling <tt>model.off()</tt>, for example, will indeed remove <i>all</i> events
on the model &mdash; including events that Backbone uses for internal bookkeeping.
</p>
<p id="Events-trigger">
<b class="header">trigger</b><code>object.trigger(event, [*args])</code>
<br />
Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
Subsequent arguments to <b>trigger</b> will be passed along to the
event callbacks.
</p>
<p id="Events-once">
<b class="header">once</b><code>object.once(event, callback, [context])</code>
<br />
Just like <a href="#Events-on">on</a>, but causes the bound callback to fire
only once before being removed. Handy for saying "the next time that X happens, do this".
When multiple events are passed in using the space separated syntax, the event will fire once
for every event you passed in, not once for a combination of all events
</p>
<p id="Events-listenTo">
<b class="header">listenTo</b><code>object.listenTo(other, event, callback)</code>
<br />
Tell an <b>object</b> to listen to a particular event on an <b>other</b>
object. The advantage of using this form, instead of <tt>other.on(event,
callback, object)</tt>, is that <b>listenTo</b> allows the <b>object</b>
to keep track of the events, and they can be removed all at once later
on. The <b>callback</b> will always be called with <b>object</b> as
context.
</p>
<pre>
view.listenTo(model, 'change', view.render);
</pre>
<p id="Events-stopListening">
<b class="header">stopListening</b><code>object.stopListening([other], [event], [callback])</code>
<br />
Tell an <b>object</b> to stop listening to events. Either call
<b>stopListening</b> with no arguments to have the <b>object</b> remove
all of its <a href="#Events-listenTo">registered</a> callbacks ... or be more
precise by telling it to remove just the events it's listening to on a
specific object, or a specific event, or just a specific callback.
</p>
<pre>
view.stopListening();
view.stopListening(model);
</pre>
<p id="Events-listenToOnce">
<b class="header">listenToOnce</b><code>object.listenToOnce(other, event, callback)</code>
<br />
Just like <a href="#Events-listenTo">listenTo</a>, but causes the bound
callback to fire only once before being removed.
</p>
<p id="Events-catalog">
<b class="header">Catalog of Events</b>
<br />
Here's the complete list of built-in Backbone events, with arguments.
You're also free to trigger your own events on Models, Collections and
Views as you see fit. The <tt>Backbone</tt> object itself mixes in <tt>Events</tt>,
and can be used to emit any global events that your application needs.
</p>
<ul class="small">
<li><b>"add"</b> (model, collection, options) &mdash; when a model is added to a collection.</li>
<li><b>"remove"</b> (model, collection, options) &mdash; when a model is removed from a collection.</li>
<li><b>"update"</b> (collection, options) &mdash; single event triggered after any number of models have been added, removed or changed in a collection.</li>
<li><b>"reset"</b> (collection, options) &mdash; when the collection's entire contents have been <a href="#Collection-reset">reset</a>.</li>
<li><b>"sort"</b> (collection, options) &mdash; when the collection has been re-sorted.</li>
<li><b>"change"</b> (model, options) &mdash; when a model's attributes have changed.</li>
<li><b>"change:[attribute]"</b> (model, value, options) &mdash; when a specific attribute has been updated.</li>
<li><b>"destroy"</b> (model, collection, options) &mdash; when a model is <a href="#Model-destroy">destroyed</a>.</li>
<li><b>"request"</b> (model_or_collection, xhr, options) &mdash; when a model or collection has started a request to the server.</li>
<li><b>"sync"</b> (model_or_collection, response, options) &mdash; when a model or collection has been successfully synced with the server.</li>
<li><b>"error"</b> (model_or_collection, response, options) &mdash; when a model's or collection's request to the server has failed.</li>
<li><b>"invalid"</b> (model, error, options) &mdash; when a model's <a href="#Model-validate">validation</a> fails on the client.</li>
<li><b>"route:[name]"</b> (params) &mdash; Fired by the router when a specific route is matched.</li>
<li><b>"route"</b> (route, params) &mdash; Fired by the router when <i>any</i> route has been matched.</li>
<li><b>"route"</b> (router, route, params) &mdash; Fired by history when <i>any</i> route has been matched.</li>
<li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument followed by all trigger arguments.</li>
</ul>
<p>
Generally speaking, when calling a function that emits an event
(<tt>model.set</tt>, <tt>collection.add</tt>, and so on...),
if you'd like to prevent the event from being triggered, you may pass
<tt>{silent: true}</tt> as an option. Note that this is <i>rarely</i>,
perhaps even never, a good idea. Passing through a specific flag
in the options for your event callback to look at, and choose to ignore,
will usually work out better.
</p>
<h2 id="Model">Backbone.Model</h2>
<p>
<b>Models</b> are the heart of any JavaScript application, containing
the interactive data as well as a large part of the logic surrounding it:
conversions, validations, computed properties, and access control. You
extend <b>Backbone.Model</b> with your domain-specific methods, and
<b>Model</b> provides a basic set of functionality for managing changes.
</p>
<p>
The following is a contrived example, but it demonstrates defining a model
with a custom method, setting an attribute, and firing an event keyed
to changes in that specific attribute.
After running this code once, <tt>sidebar</tt> will be
available in your browser's console, so you can play around with it.
</p>
<pre class="runnable">
var Sidebar = Backbone.Model.extend({
promptColor: function() {
var cssColor = prompt("Please enter a CSS color:");
this.set({color: cssColor});
}
});
window.sidebar = new Sidebar;
sidebar.on('change:color', function(model, color) {
$('#sidebar').css({background: color});
});
sidebar.set({color: 'white'});
sidebar.promptColor();
</pre>
<p id="Model-extend">
<b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
<br />
To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
and provide instance <b>properties</b>, as well as optional
<b>classProperties</b> to be attached directly to the constructor function.
</p>
<p>
<b>extend</b> correctly sets up the prototype chain, so subclasses created
with <b>extend</b> can be further extended and subclassed as far as you like.
</p>
<pre>
var Note = Backbone.Model.extend({
initialize: function() { ... },
author: function() { ... },
coordinates: function() { ... },
allowedToEdit: function(account) {
return true;
}
});
var PrivateNote = Note.extend({
allowedToEdit: function(account) {
return account.owns(this);
}
});
</pre>
<p class="warning">
Brief aside on <tt>super</tt>: JavaScript does not provide
a simple way to call super &mdash; the function of the same name defined
higher on the prototype chain. If you override a core function like
<tt>set</tt>, or <tt>save</tt>, and you want to invoke the
parent object's implementation, you'll have to explicitly call it, along these lines:
</p>
<pre>
var Note = Backbone.Model.extend({
set: function(attributes, options) {
Backbone.Model.prototype.set.apply(this, arguments);
...
}
});
</pre>
<p id="Model-constructor">
<b class="header">constructor / initialize</b><code>new Model([attributes], [options])</code>
<br />
When creating an instance of a model, you can pass in the initial values
of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
model. If you define an <b>initialize</b> function, it will be invoked when
the model is created.
</p>
<pre>
new Book({
title: "One Thousand and One Nights",
author: "Scheherazade"
});
</pre>
<p>
In rare cases, if you're looking to get fancy,
you may want to override <b>constructor</b>, which allows
you to replace the actual constructor function for your model.
</p>
<pre>
var Library = Backbone.Model.extend({
constructor: function() {
this.books = new Books();
Backbone.Model.apply(this, arguments);
},
parse: function(data, options) {
this.books.reset(data.books);
return data.library;
}
});
</pre>
<p>
If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model
gains a <tt>collection</tt> property that will be used to indicate which
collection the model belongs to, and is used to help compute the model's
<a href="#Model-url">url</a>. The <tt>model.collection</tt> property is
normally created automatically when you first add a model to a collection.
Note that the reverse is not true, as passing this option to the constructor
will not automatically add the model to the collection. Useful, sometimes.
</p>
<p>
If <tt>{parse: true}</tt> is passed as an <b>option</b>, the <b>attributes</b>
will first be converted by <a href="#Model-parse">parse</a> before being
<a href="#Model-set">set</a> on the model.
</p>
<p id="Model-get">
<b class="header">get</b><code>model.get(attribute)</code>
<br />
Get the current value of an attribute from the model. For example:
<tt>note.get("title")</tt>
</p>
<p id="Model-set">
<b class="header">set</b><code>model.set(attributes, [options])</code>
<br />
Set a hash of attributes (one or many) on the model. If any of the attributes
change the model's state, a <tt>"change"</tt> event will be triggered on the model.
Change events for specific attributes are also triggered, and you can bind
to those as well, for example: <tt>change:title</tt>, and <tt>change:content</tt>.
You may also pass individual keys and values.
</p>
<pre>
note.set({title: "March 20", content: "In his eyes she eclipses..."});
book.set("title", "A Scandal in Bohemia");
</pre>
<p id="Model-escape">
<b class="header">escape</b><code>model.escape(attribute)</code>
<br />
Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
of a model's attribute. If you're interpolating data from the model into
HTML, using <b>escape</b> to retrieve attributes will prevent
<a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
</p>
<pre class="runnable">
var hacker = new Backbone.Model({
name: "&lt;script&gt;alert('xss')&lt;/script&gt;"
});
alert(hacker.escape('name'));
</pre>
<p id="Model-has">
<b class="header">has</b><code>model.has(attribute)</code>
<br />
Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
value.
</p>
<pre>
if (note.has("title")) {
...
}
</pre>
<p id="Model-unset">
<b class="header">unset</b><code>model.unset(attribute, [options])</code>
<br />
Remove an attribute by deleting it from the internal attributes hash.
Fires a <tt>"change"</tt> event unless <tt>silent</tt> is passed as an option.
</p>
<p id="Model-clear">
<b class="header">clear</b><code>model.clear([options])</code>
<br />
Removes all attributes from the model, including the <tt>id</tt> attribute. Fires a <tt>"change"</tt> event unless
<tt>silent</tt> is passed as an option.
</p>
<p id="Model-id">
<b class="header">id</b><code>model.id</code>
<br />
A special property of models, the <b>id</b> is an arbitrary string
(integer id or UUID). If you set the <b>id</b> in the
attributes hash, it will be copied onto the model as a direct property.
<code>model.id</code> should not be manipulated directly,
it should be modified only via <code>model.set('id', …)</code>.
Models can be retrieved by id from collections, and the id is used to generate
model URLs by default.
</p>
<p id="Model-idAttribute">
<b class="header">idAttribute</b><code>model.idAttribute</code>
<br />
A model's unique identifier is stored under the <tt>id</tt> attribute.
If you're directly communicating with a backend (CouchDB, MongoDB) that uses
a different unique key, you may set a Model's <tt>idAttribute</tt> to
transparently map from that key to <tt>id</tt>.
<pre class="runnable">
var Meal = Backbone.Model.extend({
idAttribute: "_id"
});
var cake = new Meal({ _id: 1, name: "Cake" });
alert("Cake id: " + cake.id);
</pre>
</p>
<p id="Model-cid">
<b class="header">cid</b><code>model.cid</code>
<br />
A special property of models, the <b>cid</b> or client id is a unique identifier
automatically assigned to all models when they're first created. Client ids
are handy when the model has not yet been saved to the server, and does not
yet have its eventual true <b>id</b>, but already needs to be visible in the UI.
</p>
<p id="Model-attributes">
<b class="header">attributes</b><code>model.attributes</code>
<br />
The <b>attributes</b> property is the internal hash containing the model's
state &mdash; usually (but not necessarily) a form of the JSON object
representing the model data on the server. It's often a straightforward
serialization of a row from the database, but it could also be client-side
computed state.
</p>
<p>
Please use <a href="#Model-set">set</a> to update the <b>attributes</b>
instead of modifying them directly. If you'd like to retrieve and munge a
copy of the model's attributes, use <tt>_.clone(model.attributes)</tt>
instead.
</p>
<p class="warning">
Due to the fact that <a href="#Events">Events</a> accepts space separated
lists of events, attribute names should not include spaces.
</p>
<p id="Model-changed">
<b class="header">changed</b><code>model.changed</code>
<br />
The <b>changed</b> property is the internal hash containing all the attributes
that have changed since its last <a href="#Model-set">set</a>.
Please do not update <b>changed</b> directly since its state is internally maintained
by <a href="#Model-set">set</a>. A copy of <b>changed</b> can be acquired from
<a href="#Model-changedAttributes">changedAttributes</a>.
</p>
<p id="Model-defaults">
<b class="header">defaults</b><code>model.defaults or model.defaults()</code>
<br />
The <b>defaults</b> hash (or function) can be used to specify the default
attributes for your model. When creating an instance of the model,
any unspecified attributes will be set to their default value.
</p>
<pre class="runnable">
var Meal = Backbone.Model.extend({
defaults: {
"appetizer": "caesar salad",
"entree": "ravioli",
"dessert": "cheesecake"
}
});
alert("Dessert will be " + (new Meal).get('dessert'));
</pre>
<p class="warning">
Remember that in JavaScript, objects are passed by reference, so if you
include an object as a default value, it will be shared among all instances.
Instead, define <b>defaults</b> as a function.
</p>
<p id="Model-toJSON">
<b class="header">toJSON</b><code>model.toJSON([options])</code>
<br />
Return a shallow copy of the model's <a href="#Model-attributes">attributes</a>
for JSON stringification. This can be used for persistence,
serialization, or for augmentation before being sent to the server. The
name of this method is a bit confusing, as it doesn't actually return a
JSON string &mdash; but I'm afraid that it's the way that the
<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior">JavaScript API for <b>JSON.stringify</b></a>
works.
</p>
<pre class="runnable">
var artist = new Backbone.Model({
firstName: "Wassily",
lastName: "Kandinsky"
});
artist.set({birthday: "December 16, 1866"});
alert(JSON.stringify(artist));
</pre>
<p id="Model-sync">
<b class="header">sync</b><code>model.sync(method, model, [options])</code>
<br />
Uses <a href="#Sync">Backbone.sync</a> to persist the state of a model to
the server. Can be overridden for custom behavior.
</p>
<p id="Model-fetch">
<b class="header">fetch</b><code>model.fetch([options])</code>
<br />
Merges the model's state with attributes fetched from the server by
delegating to <a href="#Sync">Backbone.sync</a>. Returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a>.
Useful if the model has never
been populated with data, or if you'd like to ensure that you have the
latest server state. Triggers a <tt>"change"</tt> event if the
server's state differs from the current attributes. <tt>fetch</tt> accepts
<tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
are both passed <tt>(model, response, options)</tt> as arguments.
</p>
<pre>
// Poll every 10 seconds to keep the channel model up-to-date.
setInterval(function() {
channel.fetch();
}, 10000);
</pre>
<p id="Model-save">
<b class="header">save</b><code>model.save([attributes], [options])</code>
<br />
Save a model to your database (or alternative persistence layer),
by delegating to <a href="#Sync">Backbone.sync</a>. Returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> if
validation is successful and <tt>false</tt> otherwise. The <b>attributes</b>
hash (as in <a href="#Model-set">set</a>) should contain the attributes
you'd like to change &mdash; keys that aren't mentioned won't be altered &mdash; but,
a <i>complete representation</i> of the resource will be sent to the server.
As with <tt>set</tt>, you may pass individual keys and values instead of a hash.
If the model has a <a href="#Model-validate">validate</a>
method, and validation fails, the model will not be saved. If the model
<a href="#Model-isNew">isNew</a>, the save will be a <tt>"create"</tt>
(HTTP <tt>POST</tt>), if the model already
exists on the server, the save will be an <tt>"update"</tt> (HTTP <tt>PUT</tt>).
</p>
<p>
If instead, you'd only like the <i>changed</i> attributes to be sent to the
server, call <tt>model.save(attrs, {patch: true})</tt>. You'll get an HTTP
<tt>PATCH</tt> request to the server with just the passed-in attributes.
</p>
<p>
Calling <tt>save</tt> with new attributes will cause a <tt>"change"</tt>
event immediately, a <tt>"request"</tt> event as the Ajax request begins to
go to the server, and a <tt>"sync"</tt> event after the server has acknowledged
the successful change. Pass <tt>{wait: true}</tt> if you'd like to wait
for the server before setting the new attributes on the model.
</p>
<p>
In the following example, notice how our overridden version
of <tt>Backbone.sync</tt> receives a <tt>"create"</tt> request
the first time the model is saved and an <tt>"update"</tt>
request the second time.
</p>
<pre class="runnable">
Backbone.sync = function(method, model) {
alert(method + ": " + JSON.stringify(model));
model.set('id', 1);
};
var book = new Backbone.Model({
title: "The Rough Riders",
author: "Theodore Roosevelt"
});
book.save();
book.save({author: "Teddy"});
</pre>
<p>
<b>save</b> accepts <tt>success</tt> and <tt>error</tt> callbacks in the
options hash, which will be passed the arguments <tt>(model, response, options)</tt>.
If a server-side validation fails, return a non-<tt>200</tt>
HTTP response code, along with an error response in text or JSON.
</p>
<pre>
book.save("author", "F.D.R.", {error: function(){ ... }});
</pre>
<p id="Model-destroy">
<b class="header">destroy</b><code>model.destroy([options])</code>
<br />
Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
request to <a href="#Sync">Backbone.sync</a>. Returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> object, or
<tt>false</tt> if the model <a href="#Model-isNew">isNew</a>. Accepts
<tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
will be passed <tt>(model, response, options)</tt>.
Triggers a <tt>"destroy"</tt> event on the model, which will bubble up
through any collections that contain it, a <tt>"request"</tt> event as it
begins the Ajax request to the server, and a <tt>"sync"</tt> event, after
the server has successfully acknowledged the model's deletion. Pass
<tt>{wait: true}</tt> if you'd like to wait for the server to respond
before removing the model from the collection.
</p>
<pre>
book.destroy({success: function(model, response) {
...
}});
</pre>
<p id="Model-Underscore-Methods">
<b class="header">Underscore Methods (9)</b>
<br />
Backbone proxies to <b>Underscore.js</b> to provide 9 object functions
on <b>Backbone.Model</b>. They aren't all documented here, but
you can take a look at the Underscore documentation for the full details&hellip;
</p>
<ul class="small">
<li><a href="http://underscorejs.org/#keys">keys</a></li>
<li><a href="http://underscorejs.org/#values">values</a></li>
<li><a href="http://underscorejs.org/#pairs">pairs</a></li>
<li><a href="http://underscorejs.org/#invert">invert</a></li>
<li><a href="http://underscorejs.org/#pick">pick</a></li>
<li><a href="http://underscorejs.org/#omit">omit</a></li>
<li><a href="http://underscorejs.org/#chain">chain</a></li>
<li><a href="http://underscorejs.org/#isEmpty">isEmpty</a></li>
</ul>
<pre>
user.pick('first_name', 'last_name', 'email');
chapters.keys().join(', ');
</pre>
<p id="Model-validate">
<b class="header">validate</b><code>model.validate(attributes, options)</code>
<br />
This method is left undefined and you're encouraged to override it with
any custom validation logic you have that can be performed in JavaScript.
By default <tt>save</tt> checks <b>validate</b> before
setting any attributes but you may also tell <tt>set</tt> to validate
the new attributes by passing <tt>{validate: true}</tt> as an option.
<br />
The <b>validate</b> method receives the model attributes as well as any
options passed to <tt>set</tt> or <tt>save</tt>.
If the attributes are valid, don't return anything from <b>validate</b>;
if they are invalid return an error of your choosing. It
can be as simple as a string error message to be displayed, or a complete
error object that describes the error programmatically. If <b>validate</b>
returns an error, <tt>save</tt> will not continue, and the
model attributes will not be modified on the server.
Failed validations trigger an <tt>"invalid"</tt> event, and set the
<tt>validationError</tt> property on the model with the value returned by
this method.
</p>
<pre class="runnable">
var Chapter = Backbone.Model.extend({
validate: function(attrs, options) {
if (attrs.end &lt; attrs.start) {
return "can't end before it starts";
}
}
});
var one = new Chapter({
title : "Chapter One: The Beginning"
});
one.on("invalid", function(model, error) {
alert(model.get("title") + " " + error);
});
one.save({
start: 15,
end: 10
});
</pre>
<p>
<tt>"invalid"</tt> events are useful for providing coarse-grained error
messages at the model or collection level.
</p>
<p id="Model-validationError">
<b class="header">validationError</b><code>model.validationError</code>
<br />
The value returned by <a href="#Model-validate">validate</a> during the last failed validation.
</p>
<p id="Model-isValid">
<b class="header">isValid</b><code>model.isValid()</code>
<br />
Run <a href="#Model-validate">validate</a> to check the model state.
</p>
<pre class="runnable">
var Chapter = Backbone.Model.extend({
validate: function(attrs, options) {
if (attrs.end &lt; attrs.start) {
return "can't end before it starts";
}
}
});
var one = new Chapter({
title : "Chapter One: The Beginning"
});
one.set({
start: 15,
end: 10
});
if (!one.isValid()) {
alert(one.get("title") + " " + one.validationError);
}
</pre>
<p id="Model-url">
<b class="header">url</b><code>model.url()</code>
<br />
Returns the relative URL where the model's resource would be located on
the server. If your models are located somewhere else, override this method
with the correct logic. Generates URLs of the form: <tt>"[collection.url]/[id]"</tt>
by default, but you may override by specifying an explicit <tt>urlRoot</tt>
if the model's collection shouldn't be taken into account.
</p>
<p>
Delegates to <a href="#Collection-url">Collection#url</a> to generate the
URL, so make sure that you have it defined, or a <a href="#Model-urlRoot">urlRoot</a>
property, if all models of this class share a common root URL.
A model with an id of <tt>101</tt>, stored in a
<a href="#Collection">Backbone.Collection</a> with a <tt>url</tt> of <tt>"/documents/7/notes"</tt>,
would have this URL: <tt>"/documents/7/notes/101"</tt>
</p>
<p id="Model-urlRoot">
<b class="header">urlRoot</b><code>model.urlRoot or model.urlRoot()</code>
<br />
Specify a <tt>urlRoot</tt> if you're using a model <i>outside</i> of a collection,
to enable the default <a href="#Model-url">url</a> function to generate
URLs based on the model id. <tt>"[urlRoot]/id"</tt><br />
Normally, you won't need to define this.
Note that <tt>urlRoot</tt> may also be a function.
</p>
<pre class="runnable">
var Book = Backbone.Model.extend({urlRoot : '/books'});
var solaris = new Book({id: "1083-lem-solaris"});
alert(solaris.url());
</pre>
<p id="Model-parse">
<b class="header">parse</b><code>model.parse(response, options)</code>
<br />
<b>parse</b> is called whenever a model's data is returned by the
server, in <a href="#Model-fetch">fetch</a>, and <a href="#Model-save">save</a>.
The function is passed the raw <tt>response</tt> object, and should return
the attributes hash to be <a href="#Model-set">set</a> on the model. The
default implementation is a no-op, simply passing through the JSON response.
Override this if you need to work with a preexisting API, or better namespace
your responses.
</p>
<p>
If you're working with a Rails backend that has a version prior to 3.1,
you'll notice that its default <tt>to_json</tt> implementation includes
a model's attributes under a namespace. To disable this behavior for
seamless Backbone integration, set:
</p>
<pre>
ActiveRecord::Base.include_root_in_json = false
</pre>
<p id="Model-clone">
<b class="header">clone</b><code>model.clone()</code>
<br />
Returns a new instance of the model with identical attributes.
</p>
<p id="Model-isNew">
<b class="header">isNew</b><code>model.isNew()</code>
<br />
Has this model been saved to the server yet? If the model does not yet have
an <tt>id</tt>, it is considered to be new.
</p>
<p id="Model-hasChanged">
<b class="header">hasChanged</b><code>model.hasChanged([attribute])</code>
<br />
Has the model changed since its last <a href="#Model-set">set</a>? If an <b>attribute</b>
is passed, returns <tt>true</tt> if that specific attribute has changed.
</p>
<p class="warning">
Note that this method, and the following change-related ones,
are only useful during the course of a <tt>"change"</tt> event.
</p>
<pre>
book.on("change", function() {
if (book.hasChanged("title")) {
...
}
});
</pre>
<p id="Model-changedAttributes">
<b class="header">changedAttributes</b><code>model.changedAttributes([attributes])</code>
<br />
Retrieve a hash of only the model's attributes that have changed since the last
<a href="#Model-set">set</a>, or <tt>false</tt> if there are none. Optionally, an external
<b>attributes</b> hash can be passed in, returning the attributes in that
hash which differ from the model. This can be used to figure out which
portions of a view should be updated, or what calls
need to be made to sync the changes to the server.
</p>
<p id="Model-previous">
<b class="header">previous</b><code>model.previous(attribute)</code>
<br />
During a <tt>"change"</tt> event, this method can be used to get the
previous value of a changed attribute.
</p>
<pre class="runnable">
var bill = new Backbone.Model({
name: "Bill Smith"
});
bill.on("change:name", function(model, name) {
alert("Changed name from " + bill.previous("name") + " to " + name);
});
bill.set({name : "Bill Jones"});
</pre>
<p id="Model-previousAttributes">
<b class="header">previousAttributes</b><code>model.previousAttributes()</code>
<br />
Return a copy of the model's previous attributes. Useful for getting a
diff between versions of a model, or getting back to a valid state after
an error occurs.
</p>
<h2 id="Collection">Backbone.Collection</h2>
<p>
Collections are ordered sets of models. You can bind <tt>"change"</tt> events
to be notified when any model in the collection has been modified,
listen for <tt>"add"</tt> and <tt>"remove"</tt> events, <tt>fetch</tt>
the collection from the server, and use a full suite of
<a href="#Collection-Underscore-Methods">Underscore.js methods</a>.
</p>
<p>
Any event that is triggered on a model in a collection will also be
triggered on the collection directly, for convenience.
This allows you to listen for changes to specific attributes in any
model in a collection, for example:
<tt>documents.on("change:selected", ...)</tt>
</p>
<p id="Collection-extend">
<b class="header">extend</b><code>Backbone.Collection.extend(properties, [classProperties])</code>
<br />
To create a <b>Collection</b> class of your own, extend <b>Backbone.Collection</b>,
providing instance <b>properties</b>, as well as optional <b>classProperties</b> to be attached
directly to the collection's constructor function.
</p>
<p id="Collection-model">
<b class="header">model</b><code>collection.model([attrs], [options])</code>
<br />
Override this property to specify the model class that the collection
contains. If defined, you can pass raw attributes objects (and arrays) to
<a href="#Collection-add">add</a>, <a href="#Collection-create">create</a>,
and <a href="#Collection-reset">reset</a>, and the attributes will be
converted into a model of the proper type.
</p>
<pre>
var Library = Backbone.Collection.extend({
model: Book
});
</pre>
<p>
A collection can also contain polymorphic models by overriding this property
with a constructor that returns a model.
</p>
<pre>
var Library = Backbone.Collection.extend({
model: function(attrs, options) {
if (condition) {
return new PublicDocument(attrs, options);
} else {
return new PrivateDocument(attrs, options);
}
}
});
</pre>
<p id="Collection-modelId">
<b class="header">modelId</b><code>collection.modelId(attrs)</code>
<br />
Override this method to return the value the collection will use to
identify a model given its attributes. Useful for combining models from
multiple tables with different <a href="#Model-idAttribute"><tt>idAttribute</tt></a>
values into a single collection.
</p>
<p>
By default returns the value of the attributes'
<a href="#Model-idAttribute"><tt>idAttribute</tt></a>
from the collection's model class or failing that, <tt>id</tt>. If
your collection uses a <a href="#Collection-model">model factory</a> and
those models have an <tt>idAttribute</tt> other than <tt>id</tt> you must
override this method.
</p>
<pre class="runnable">
var Library = Backbone.Collection.extend({
modelId: function(attrs) {
return attrs.type + attrs.id;
}
});
var library = new Library([
{type: 'dvd', id: 1},
{type: 'vhs', id: 1}
]);
var dvdId = library.get('dvd1').id;
var vhsId = library.get('vhs1').id;
alert('dvd: ' + dvdId + ', vhs: ' + vhsId);
</pre>
<p id="Collection-constructor">
<b class="header">constructor / initialize</b><code>new Backbone.Collection([models], [options])</code>
<br />
When creating a Collection, you may choose to pass in the initial array
of <b>models</b>. The collection's <a href="#Collection-comparator">comparator</a>
may be included as an option. Passing <tt>false</tt> as the
comparator option will prevent sorting. If you define an
<b>initialize</b> function, it will be invoked when the collection is
created. There are a couple of options that, if provided, are attached to
the collection directly: <tt>model</tt> and <tt>comparator</tt>.<br />
Pass <tt>null</tt> for <tt>models</tt> to create an empty Collection with <tt>options</tt>.
</p>
<pre>
var tabs = new TabSet([tab1, tab2, tab3]);
var spaces = new Backbone.Collection(null, {
model: Space
});
</pre>
<p id="Collection-models">
<b class="header">models</b><code>collection.models</code>
<br />
Raw access to the JavaScript array of models inside of the collection. Usually you'll
want to use <tt>get</tt>, <tt>at</tt>, or the <b>Underscore methods</b>
to access model objects, but occasionally a direct reference to the array
is desired.
</p>
<p id="Collection-toJSON">
<b class="header">toJSON</b><code>collection.toJSON([options])</code>
<br />
Return an array containing the attributes hash of each model
(via <a href="#Model-toJSON">toJSON</a>) in the
collection. This can be used to serialize and persist the
collection as a whole. The name of this method is a bit confusing, because
it conforms to
<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior">JavaScript's JSON API</a>.
</p>
<pre class="runnable">
var collection = new Backbone.Collection([
{name: "Tim", age: 5},
{name: "Ida", age: 26},
{name: "Rob", age: 55}
]);
alert(JSON.stringify(collection));
</pre>
<p id="Collection-sync">
<b class="header">sync</b><code>collection.sync(method, collection, [options])</code>
<br />
Uses <a href="#Sync">Backbone.sync</a> to persist the state of a
collection to the server. Can be overridden for custom behavior.
</p>
<p id="Collection-Underscore-Methods">
<b class="header">Underscore Methods (46)</b>
<br />
Backbone proxies to <b>Underscore.js</b> to provide 46 iteration functions
on <b>Backbone.Collection</b>. They aren't all documented here, but
you can take a look at the Underscore documentation for the full details&hellip;
</p>
<p>
Most methods can take an object or string to support model-attribute-style
predicates or a function that receives the model instance as an argument.
</p>
<ul class="small">
<li><a href="http://underscorejs.org/#each">forEach (each)</a></li>
<li><a href="http://underscorejs.org/#map">map (collect)</a></li>
<li><a href="http://underscorejs.org/#reduce">reduce (foldl, inject)</a></li>
<li><a href="http://underscorejs.org/#reduceRight">reduceRight (foldr)</a></li>
<li><a href="http://underscorejs.org/#find">find (detect)</a></li>
<li><a href="http://underscorejs.org/#findIndex">findIndex</a></li>
<li><a href="http://underscorejs.org/#findLastIndex">findLastIndex</a></li>
<li><a href="http://underscorejs.org/#filter">filter (select)</a></li>
<li><a href="http://underscorejs.org/#reject">reject</a></li>
<li><a href="http://underscorejs.org/#every">every (all)</a></li>
<li><a href="http://underscorejs.org/#some">some (any)</a></li>
<li><a href="http://underscorejs.org/#contains">contains (includes)</a></li>
<li><a href="http://underscorejs.org/#invoke">invoke</a></li>
<li><a href="http://underscorejs.org/#max">max</a></li>
<li><a href="http://underscorejs.org/#min">min</a></li>
<li><a href="http://underscorejs.org/#sortBy">sortBy</a></li>
<li><a href="http://underscorejs.org/#groupBy">groupBy</a></li>
<li><a href="http://underscorejs.org/#shuffle">shuffle</a></li>
<li><a href="http://underscorejs.org/#toArray">toArray</a></li>
<li><a href="http://underscorejs.org/#size">size</a></li>
<li><a href="http://underscorejs.org/#first">first (head, take)</a></li>
<li><a href="http://underscorejs.org/#initial">initial</a></li>
<li><a href="http://underscorejs.org/#rest">rest (tail, drop)</a></li>
<li><a href="http://underscorejs.org/#last">last</a></li>
<li><a href="http://underscorejs.org/#without">without</a></li>
<li><a href="http://underscorejs.org/#indexOf">indexOf</a></li>
<li><a href="http://underscorejs.org/#lastIndexOf">lastIndexOf</a></li>
<li><a href="http://underscorejs.org/#isEmpty">isEmpty</a></li>
<li><a href="http://underscorejs.org/#chain">chain</a></li>
<li><a href="http://underscorejs.org/#difference">difference</a></li>
<li><a href="http://underscorejs.org/#sample">sample</a></li>
<li><a href="http://underscorejs.org/#partition">partition</a></li>
<li><a href="http://underscorejs.org/#countBy">countBy</a></li>
<li><a href="http://underscorejs.org/#indexBy">indexBy</a></li>
</ul>
<pre>
books.each(function(book) {
book.publish();
});
var titles = books.map("title");
var publishedBooks = books.filter({published: true});
var alphabetical = books.sortBy(function(book) {
return book.author.get("name").toLowerCase();
});
var randomThree = books.sample(3);
</pre>
<p id="Collection-add">
<b class="header">add</b><code>collection.add(models, [options])</code>
<br />
Add a model (or an array of models) to the collection, firing an <tt>"add"</tt>
event for each model, and an <tt>"update"</tt> event afterwards. If a <a href="#Collection-model">model</a> property is defined, you may also pass
raw attributes objects, and have them be vivified as instances of the model.
Returns the added (or preexisting, if duplicate) models.
Pass <tt>{at: index}</tt> to splice the model into the collection at the
specified <tt>index</tt>. If you're adding models to the collection that are
<i>already</i> in the collection, they'll be ignored, unless you pass
<tt>{merge: true}</tt>, in which case their attributes will be merged
into the corresponding models, firing any appropriate <tt>"change"</tt> events.
</p>
<pre class="runnable">
var ships = new Backbone.Collection;
ships.on("add", function(ship) {
alert("Ahoy " + ship.get("name") + "!");
});
ships.add([
{name: "Flying Dutchman"},
{name: "Black Pearl"}
]);
</pre>
<p class="warning">
Note that adding the same model (a model with the same <tt>id</tt>) to
a collection more than once <br /> is a no-op.
</p>
<p id="Collection-remove">
<b class="header">remove</b><code>collection.remove(models, [options])</code>
<br />
Remove a model (or an array of models) from the collection, and return
them. Each model can be a Model instance, an <tt>id</tt> string or a JS
object, any value acceptable as the <tt>id</tt> argument of
<a href="#Collection-get"><tt>collection.get</tt></a>.
Fires a <tt>"remove"</tt> event for each model, and a single
<tt>"update"</tt> event afterwards, unless <tt>{silent: true}</tt> is passed.
The model's index before removal is available to listeners as
<tt>options.index</tt>.
</p>
<p id="Collection-reset">
<b class="header">reset</b><code>collection.reset([models], [options])</code>
<br />
Adding and removing models one at a time is all well and good, but sometimes
you have so many models to change that you'd rather just update the collection
in bulk. Use <b>reset</b> to replace a collection with a new list
of models (or attribute hashes), triggering a single <tt>"reset"</tt> event
on completion, and <i>without</i> triggering any add or remove events on any models.
Returns the newly-set models.
For convenience, within a <tt>"reset"</tt> event, the list of any
previous models is available as <tt>options.previousModels</tt>.<br />
Pass <tt>null</tt> for <tt>models</tt> to empty your Collection with <tt>options</tt>.
</p>
<p>
Here's an example using <b>reset</b> to bootstrap a collection during initial page load,
in a Rails application:
</p>
<pre>
&lt;script&gt;
var accounts = new Backbone.Collection;
accounts.reset(&lt;%= @accounts.to_json %&gt;);
&lt;/script&gt;
</pre>
<p>
Calling <tt>collection.reset()</tt> without passing any models as arguments
will empty the entire collection.
</p>
<p id="Collection-set">
<b class="header">set</b><code>collection.set(models, [options])</code>
<br />
The <b>set</b> method performs a "smart" update of the collection
with the passed list of models. If a model in the list isn't yet in the
collection it will be added; if the model is already in the collection
its attributes will be merged; and if the collection contains any models that
<i>aren't</i> present in the list, they'll be removed. All of the appropriate
<tt>"add"</tt>, <tt>"remove"</tt>, and <tt>"change"</tt> events are fired
as this happens. Returns the touched models in the collection.
If you'd like to customize the behavior, you can disable
it with options: <tt>{add: false}</tt>, <tt>{remove: false}</tt>, or <tt>{merge: false}</tt>.
</p>
<pre>
var vanHalen = new Backbone.Collection([eddie, alex, stone, roth]);
vanHalen.set([eddie, alex, stone, hagar]);
// Fires a "remove" event for roth, and an "add" event for "hagar".
// Updates any of stone, alex, and eddie's attributes that may have
// changed over the years.
</pre>
<p id="Collection-get">
<b class="header">get</b><code>collection.get(id)</code>
<br />
Get a model from a collection, specified by an <a href="#Model-id">id</a>,
a <a href="#Model-cid">cid</a>, or by passing in a <b>model</b>.
</p>
<pre>
var book = library.get(110);
</pre>
<p id="Collection-at">
<b class="header">at</b><code>collection.at(index)</code>
<br />
Get a model from a collection, specified by index. Useful if your collection
is sorted, and if your collection isn't sorted, <b>at</b> will still
retrieve models in insertion order. When passed a negative index, it
will retrieve the model from the back of the collection.
</p>
<p id="Collection-push">
<b class="header">push</b><code>collection.push(model, [options])</code>
<br />
Add a model at the end of a collection. Takes the same options as
<a href="#Collection-add">add</a>.
</p>
<p id="Collection-pop">
<b class="header">pop</b><code>collection.pop([options])</code>
<br />
Remove and return the last model from a collection. Takes the same options as
<a href="#Collection-remove">remove</a>.
</p>
<p id="Collection-unshift">
<b class="header">unshift</b><code>collection.unshift(model, [options])</code>
<br />
Add a model at the beginning of a collection. Takes the same options as
<a href="#Collection-add">add</a>.
</p>
<p id="Collection-shift">
<b class="header">shift</b><code>collection.shift([options])</code>
<br />
Remove and return the first model from a collection. Takes the same options as
<a href="#Collection-remove">remove</a>.
</p>
<p id="Collection-slice">
<b class="header">slice</b><code>collection.slice(begin, end)</code>
<br />
Return a shallow copy of this collection's models, using the same options as
native
<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">Array#slice</a>.
</p>
<p id="Collection-length">
<b class="header">length</b><code>collection.length</code>
<br />
Like an array, a Collection maintains a <tt>length</tt> property, counting
the number of models it contains.
</p>
<p id="Collection-comparator">
<b class="header">comparator</b><code>collection.comparator</code>
<br />
By default there is no <b>comparator</b> for a collection.
If you define a comparator, it will be used to maintain
the collection in sorted order. This means that as models are added,
they are inserted at the correct index in <tt>collection.models</tt>.
A comparator can be defined as a
<a href="http://underscorejs.org/#sortBy">sortBy</a>
(pass a function that takes a single argument),
as a
<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">sort</a>
(pass a comparator function that expects two arguments),
or as a string indicating the attribute to sort by.
</p>
<p>
"sortBy" comparator functions take a model and return a numeric or string
value by which the model should be ordered relative to others.
"sort" comparator functions take two models, and return <tt>-1</tt> if
the first model should come before the second, <tt>0</tt> if they are of
the same rank and <tt>1</tt> if the first model should come after.
<i>Note that Backbone depends on the arity of your comparator function to
determine between the two styles, so be careful if your comparator function
is bound.</i>
</p>
<p>
Note how even though all of the chapters in this example are added backwards,
they come out in the proper order:
</p>
<pre class="runnable">
var Chapter = Backbone.Model;
var chapters = new Backbone.Collection;
chapters.comparator = 'page';
chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));
alert(chapters.pluck('title'));
</pre>
<p class="warning">
Collections with a comparator will not automatically re-sort if you
later change model attributes, so you may wish to call
<tt>sort</tt> after changing model attributes that would affect the order.
</p>
<p id="Collection-sort">
<b class="header">sort</b><code>collection.sort([options])</code>
<br />
Force a collection to re-sort itself. You don't need to call this under
normal circumstances, as a collection with a <a href="#Collection-comparator">comparator</a>
will sort itself whenever a model is added. To disable sorting when adding
a model, pass <tt>{sort: false}</tt> to <tt>add</tt>. Calling <b>sort</b>
triggers a <tt>"sort"</tt> event on the collection.
</p>
<p id="Collection-pluck">
<b class="header">pluck</b><code>collection.pluck(attribute)</code>
<br />
Pluck an attribute from each model in the collection. Equivalent to calling
<tt>map</tt> and returning a single attribute from the iterator.
</p>
<pre class="runnable">
var stooges = new Backbone.Collection([
{name: "Curly"},
{name: "Larry"},
{name: "Moe"}
]);
var names = stooges.pluck("name");
alert(JSON.stringify(names));
</pre>
<p id="Collection-where">
<b class="header">where</b><code>collection.where(attributes)</code>
<br />
Return an array of all the models in a collection that match the
passed <b>attributes</b>. Useful for simple cases of <tt>filter</tt>.
</p>
<pre class="runnable">
var friends = new Backbone.Collection([
{name: "Athos", job: "Musketeer"},
{name: "Porthos", job: "Musketeer"},
{name: "Aramis", job: "Musketeer"},
{name: "d'Artagnan", job: "Guard"},
]);
var musketeers = friends.where({job: "Musketeer"});
alert(musketeers.length);
</pre>
<p id="Collection-findWhere">
<b class="header">findWhere</b><code>collection.findWhere(attributes)</code>
<br />
Just like <a href="#Collection-where">where</a>, but directly returns only
the first model in the collection that matches the passed <b>attributes</b>.
If no model matches returns <tt>undefined</tt>.
</p>
<p id="Collection-url">
<b class="header">url</b><code>collection.url or collection.url()</code>
<br />
Set the <b>url</b> property (or function) on a collection to reference
its location on the server. Models within the collection will use <b>url</b>
to construct URLs of their own.
</p>
<pre>
var Notes = Backbone.Collection.extend({
url: '/notes'
});
// Or, something more sophisticated:
var Notes = Backbone.Collection.extend({
url: function() {
return this.document.url() + '/notes';
}
});
</pre>
<p id="Collection-parse">
<b class="header">parse</b><code>collection.parse(response, options)</code>
<br />
<b>parse</b> is called by Backbone whenever a collection's models are
returned by the server, in <a href="#Collection-fetch">fetch</a>.
The function is passed the raw <tt>response</tt> object, and should return
the array of model attributes to be <a href="#Collection-add">added</a>
to the collection. The default implementation is a no-op, simply passing
through the JSON response. Override this if you need to work with a
preexisting API, or better namespace your responses.
</p>
<pre>
var Tweets = Backbone.Collection.extend({
// The Twitter Search API returns tweets under "results".
parse: function(response) {
return response.results;
}
});
</pre>
<p id="Collection-clone">
<b class="header">clone</b><code>collection.clone()</code>
<br />
Returns a new instance of the collection with an identical list of models.
</p>
<p id="Collection-fetch">
<b class="header">fetch</b><code>collection.fetch([options])</code>
<br />
Fetch the default set of models for this collection from the server,
<a href="#Collection-set">setting</a> them on the collection when they arrive.
The <b>options</b> hash takes <tt>success</tt> and <tt>error</tt> callbacks
which will both be passed <tt>(collection, response, options)</tt> as arguments.
When the model data returns from the server, it uses <a href="#Collection-set">set</a>
to (intelligently) merge the fetched models, unless you pass <tt>{reset: true}</tt>,
in which case the collection will be (efficiently) <a href="#Collection-reset">reset</a>.
Delegates to <a href="#Sync">Backbone.sync</a>
under the covers for custom persistence strategies and returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a>.
The server handler for <b>fetch</b> requests should return a JSON array of
models.
</p>
<pre class="runnable">
Backbone.sync = function(method, model) {
alert(method + ": " + model.url);
};
var accounts = new Backbone.Collection;
accounts.url = '/accounts';
accounts.fetch();
</pre>
<p>
The behavior of <b>fetch</b> can be customized by using the available
<a href="#Collection-set">set</a> options. For example, to fetch a
collection, getting an <tt>"add"</tt> event for every new model, and
a <tt>"change"</tt> event for every changed existing model, without
removing anything: <tt>collection.fetch({remove: false})</tt>
</p>
<p>
<b>jQuery.ajax</b> options can also be passed directly as <b>fetch</b> options,
so to fetch a specific page of a paginated collection:
<tt>Documents.fetch({data: {page: 3}})</tt>
</p>
<p>
Note that <b>fetch</b> should not be used to populate collections on
page load &mdash; all models needed at load time should already be
<a href="#FAQ-bootstrap">bootstrapped</a> in to place. <b>fetch</b> is
intended for lazily-loading models for interfaces that are not needed
immediately: for example, documents with collections of notes that may be
toggled open and closed.
</p>
<p id="Collection-create">
<b class="header">create</b><code>collection.create(attributes, [options])</code>
<br />
Convenience to create a new instance of a model within a collection.
Equivalent to instantiating a model with a hash of attributes,
saving the model to the server, and adding the model to the set after being
successfully created. Returns the new model. If client-side validation
failed, the model will be unsaved, with validation errors.
In order for this to work, you should set the
<a href="#Collection-model">model</a> property of the collection.
The <b>create</b> method can accept either an attributes hash or an
existing, unsaved model object.
</p>
<p>
Creating a model will cause an immediate <tt>"add"</tt> event to be
triggered on the collection, a <tt>"request"</tt> event as the new model is
sent to the server, as well as a <tt>"sync"</tt> event, once the
server has responded with the successful creation of the model. Pass <tt>{wait: true}</tt>
if you'd like to wait for the server before adding the new model to the collection.
</p>
<pre>
var Library = Backbone.Collection.extend({
model: Book
});
var nypl = new Library;
var othello = nypl.create({
title: "Othello",
author: "William Shakespeare"
});
</pre>
<h2 id="Router">Backbone.Router</h2>
<p>
Web applications often provide linkable, bookmarkable, shareable URLs for
important locations in the app. Until recently, hash fragments
(<tt>#page</tt>) were used to provide these permalinks, but with the
arrival of the History API, it's now possible to use standard URLs (<tt>/page</tt>).
<b>Backbone.Router</b> provides methods for routing client-side pages, and
connecting them to actions and events. For browsers which don't yet support
the History API, the Router handles graceful fallback and transparent
translation to the fragment version of the URL.
</p>
<p>
During page load, after your application has finished creating all of its routers,
be sure to call <tt>Backbone.history.start()</tt> or
<tt>Backbone.history.start({pushState: true})</tt> to route the initial URL.
</p>
<p id="Router-extend">
<b class="header">extend</b><code>Backbone.Router.extend(properties, [classProperties])</code>
<br />
Get started by creating a custom router class. Define actions that are
triggered when certain URL fragments are
matched, and provide a <a href="#Router-routes">routes</a> hash
that pairs routes to actions. Note that you'll want to avoid using a
leading slash in your route definitions:
</p>
<pre>
var Workspace = Backbone.Router.extend({
routes: {
"help": "help", // #help
"search/:query": "search", // #search/kiwis
"search/:query/p:page": "search" // #search/kiwis/p7
},
help: function() {
...
},
search: function(query, page) {
...
}
});
</pre>
<p id="Router-routes">
<b class="header">routes</b><code>router.routes</code>
<br />
The routes hash maps URLs with parameters to functions on your router
(or just direct function definitions, if you prefer),
similar to the <a href="#View">View</a>'s <a href="#View-delegateEvents">events hash</a>.
Routes can contain parameter parts, <tt>:param</tt>, which match a single URL
component between slashes; and splat parts <tt>*splat</tt>, which can match
any number of URL components. Part of a route can be made optional by
surrounding it in parentheses <tt>(/:optional)</tt>.
</p>
<p>
For example, a route of <tt>"search/:query/p:page"</tt> will match
a fragment of <tt>#search/obama/p2</tt>, passing <tt>"obama"</tt>
and <tt>"2"</tt> to the action.
</p>
<p>
A route of <tt>"file/*path"</tt> will match
<tt>#file/folder/file.txt</tt>, passing
<tt>"folder/file.txt"</tt> to the action.
</p>
<p>
A route of <tt>"docs/:section(/:subsection)"</tt> will match
<tt>#docs/faq</tt> and <tt>#docs/faq/installing</tt>, passing
<tt>"faq"</tt> to the action in the first case, and passing <tt>"faq"</tt>
and <tt>"installing"</tt> to the action in the second.
</p>
<p>
A nested optional route of <tt>"docs(/:section)(/:subsection)"</tt> will match
<tt>#docs</tt>, <tt>#docs/faq</tt>, and <tt>#docs/faq/installing</tt>,
passing <tt>"faq"</tt> to the action in the second case, and passing <tt>"faq"</tt>
and <tt>"installing"</tt> to the action in the third.
</p>
<p>
Trailing slashes are treated as part of the URL, and (correctly) treated
as a unique route when accessed. <tt>docs</tt> and <tt>docs/</tt> will fire
different callbacks. If you can't avoid generating both types of URLs, you
can define a <tt>"docs(/)"</tt> matcher to capture both cases.
</p>
<p>
When the visitor presses the back button, or enters a URL, and a particular
route is matched, the name of the action will be fired as an
<a href="#Events">event</a>, so that other objects can listen to the router,
and be notified. In the following example, visiting <tt>#help/uploading</tt>
will fire a <tt>route:help</tt> event from the router.
</p>
<pre>
routes: {
"help/:page": "help",
"download/*path": "download",
"folder/:name": "openFolder",
"folder/:name-:mode": "openFolder"
}
</pre>
<pre>
router.on("route:help", function(page) {
...
});
</pre>
<p id="Router-constructor">
<b class="header">constructor / initialize</b><code>new Router([options])</code>
<br />
When creating a new router, you may pass its
<a href="#Router-routes">routes</a> hash directly as an option, if you
choose. All <tt>options</tt> will also be passed to your <tt>initialize</tt>
function, if defined.
</p>
<p id="Router-route">
<b class="header">route</b><code>router.route(route, name, [callback])</code>
<br />
Manually create a route for the router, The <tt>route</tt> argument may
be a <a href="#Router-routes">routing string</a> or regular expression.
Each matching capture from the route or regular expression will be passed as
an argument to the callback. The <tt>name</tt> argument will be triggered as
a <tt>"route:name"</tt> event whenever the route is matched. If the
<tt>callback</tt> argument is omitted <tt>router[name]</tt> will be used
instead. Routes added later may override previously declared routes.
</p>
<pre>
initialize: function(options) {
// Matches #page/10, passing "10"
this.route("page/:number", "page", function(number){ ... });
// Matches /117-a/b/c/open, passing "117-a/b/c" to this.open
this.route(/^(.*?)\/open$/, "open");
},
open: function(id) { ... }
</pre>
<p id="Router-navigate">
<b class="header">navigate</b><code>router.navigate(fragment, [options])</code>
<br />
Whenever you reach a point in your application that you'd like to save
as a URL, call <b>navigate</b> in order to update the URL.
If you also wish to call the route function, set the <b>trigger</b>
option to <tt>true</tt>.
To update the URL without creating an entry in the browser's history,
set the <b>replace</b> option to <tt>true</tt>.
</p>
<pre>
openPage: function(pageNumber) {
this.document.pages.at(pageNumber).open();
this.navigate("page/" + pageNumber);
}
# Or ...
app.navigate("help/troubleshooting", {trigger: true});
# Or ...
app.navigate("help/troubleshooting", {trigger: true, replace: true});
</pre>
<p id="Router-execute">
<b class="header">execute</b><code>router.execute(callback, args, name)</code>
<br />
This method is called internally within the router, whenever a route
matches and its corresponding <b>callback</b> is about to be executed.
Return <b>false</b> from execute to cancel the current transition.
Override it to perform custom parsing or wrapping of your routes, for
example, to parse query strings before handing them to your route
callback, like so:
</p>
<pre>
var Router = Backbone.Router.extend({
execute: function(callback, args, name) {
if (!loggedIn) {
goToLogin();
return false;
}
args.push(parseQueryString(args.pop()));
if (callback) callback.apply(this, args);
}
});
</pre>
<h2 id="History">Backbone.history</h2>
<p>
<b>History</b> serves as a global router (per frame) to handle <tt>hashchange</tt>
events or <tt>pushState</tt>, match the appropriate route, and trigger callbacks.
You shouldn't ever have to create one of these yourself since <tt>Backbone.history</tt>
already contains one.
</p>
<p>
<b>pushState</b> support exists on a purely opt-in basis in Backbone.
Older browsers that don't support <tt>pushState</tt> will continue to use
hash-based URL fragments, and if a hash URL is visited by a
<tt>pushState</tt>-capable browser, it will be transparently upgraded to
the true URL. Note that using real URLs requires your web server to be
able to correctly render those pages, so back-end changes are required
as well. For example, if you have a route of <tt>/documents/100</tt>,
your web server must be able to serve that page, if the browser
visits that URL directly. For full search-engine crawlability, it's best to
have the server generate the complete HTML for the page ... but if it's a web
application, just rendering the same content you would have for the root URL,
and filling in the rest with Backbone Views and JavaScript works fine.
</p>
<p id="History-start">
<b class="header">start</b><code>Backbone.history.start([options])</code>
<br />
When all of your <a href="#Router">Routers</a> have been created,
and all of the routes are set up properly, call <tt>Backbone.history.start()</tt>
to begin monitoring <tt>hashchange</tt> events, and dispatching routes.
Subsequent calls to <tt>Backbone.history.start()</tt> will throw an error,
and <tt>Backbone.History.started</tt> is a boolean value indicating whether
it has already been called.
</p>
<p>
To indicate that you'd like to use HTML5 <tt>pushState</tt> support in
your application, use <tt>Backbone.history.start({pushState: true})</tt>.
If you'd like to use <tt>pushState</tt>, but have browsers that don't support
it natively use full page refreshes instead, you can add
<tt>{hashChange: false}</tt> to the options.
</p>
<p>
If your application is not being served from the root url <tt>/</tt> of your
domain, be sure to tell History where the root really is, as an option:
<tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt>
</p>
<p>
When called, if a route succeeds with a match for the current URL,
<tt>Backbone.history.start()</tt> returns <tt>true</tt>. If no defined
route matches the current URL, it returns <tt>false</tt>.
</p>
<p>
If the server has already rendered the entire page, and you don't want the
initial route to trigger when starting History, pass <tt>silent: true</tt>.
</p>
<p>
Because hash-based history in Internet Explorer relies on an
<tt>&lt;iframe&gt;</tt>, be sure to call <tt>start()</tt> only after the DOM
is ready.
</p>
<pre>
$(function(){
new WorkspaceRouter();
new HelpPaneRouter();
Backbone.history.start({pushState: true});
});
</pre>
<h2 id="Sync">Backbone.sync</h2>
<p>
<b>Backbone.sync</b> is the function that Backbone calls every time it
attempts to read or save a model to the server. By default, it uses
<tt>jQuery.ajax</tt> to make a RESTful JSON request and returns a
<a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a>. You can override
it in order to use a different persistence strategy, such as WebSockets,
XML transport, or Local Storage.
</p>
<p>
The method signature of <b>Backbone.sync</b> is <tt>sync(method, model, [options])</tt>
</p>
<ul>
<li><b>method</b> – the CRUD method (<tt>"create"</tt>, <tt>"read"</tt>, <tt>"update"</tt>, or <tt>"delete"</tt>)</li>
<li><b>model</b> – the model to be saved (or collection to be read)</li>
<li><b>options</b> – success and error callbacks, and all other jQuery request options</li>
</ul>
<p>
With the default implementation, when <b>Backbone.sync</b> sends up a request to save
a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body
with content-type <tt>application/json</tt>. When returning a JSON response,
send down the attributes of the model that have been changed by the server, and need
to be updated on the client. When responding to a <tt>"read"</tt> request from a collection
(<a href="#Collection-fetch">Collection#fetch</a>), send down an array
of model attribute objects.
</p>
<p>
Whenever a model or collection begins a <b>sync</b> with the server, a
<tt>"request"</tt> event is emitted. If the request completes successfully
you'll get a <tt>"sync"</tt> event, and an <tt>"error"</tt> event if not.
</p>
<p>
The <b>sync</b> function may be overridden globally as <tt>Backbone.sync</tt>,
or at a finer-grained level, by adding a <tt>sync</tt> function to a Backbone
collection or to an individual model.
</p>
<p>
The default <b>sync</b> handler maps CRUD to REST like so:
</p>
<ul>
<li><b>create &rarr; POST &nbsp; </b><tt>/collection</tt></li>
<li><b>read &rarr; GET &nbsp; </b><tt>/collection[/id]</tt></li>
<li><b>update &rarr; PUT &nbsp; </b><tt>/collection/id</tt></li>
<li><b>patch &rarr; PATCH &nbsp; </b><tt>/collection/id</tt></li>
<li><b>delete &rarr; DELETE &nbsp; </b><tt>/collection/id</tt></li>
</ul>
<p>
As an example, a Rails 4 handler responding to an <tt>"update"</tt> call from
<tt>Backbone</tt> might look like this:
</p>
<pre>
def update
account = Account.find params[:id]
permitted = params.require(:account).permit(:name, :otherparam)
account.update_attributes permitted
render :json => account
end
</pre>
<p>
One more tip for integrating Rails versions prior to 3.1 is to disable
the default namespacing for <tt>to_json</tt> calls on models by setting
<tt>ActiveRecord::Base.include_root_in_json = false</tt>
</p>
<p id="Sync-ajax">
<b class="header">ajax</b><code>Backbone.ajax = function(request) { ... };</code>
<br />
If you want to use a custom AJAX function, or your endpoint doesn't support
the <a href="http://api.jquery.com/jQuery.ajax/">jQuery.ajax</a> API
and you need to tweak things, you can do so by setting <tt>Backbone.ajax</tt>.
</p>
<p id="Sync-emulateHTTP">
<b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code>
<br />
If you want to work with a legacy web server that doesn't support Backbone's
default REST/HTTP approach, you may choose to turn on <tt>Backbone.emulateHTTP</tt>.
Setting this option will fake <tt>PUT</tt>, <tt>PATCH</tt> and <tt>DELETE</tt> requests with
a HTTP <tt>POST</tt>, setting the <tt>X-HTTP-Method-Override</tt> header
with the true method. If <tt>emulateJSON</tt> is also on, the true method
will be passed as an additional <tt>_method</tt> parameter.
</p>
<pre>
Backbone.emulateHTTP = true;
model.save(); // POST to "/collection/id", with "_method=PUT" + header.
</pre>
<p id="Sync-emulateJSON">
<b class="header">emulateJSON</b><code>Backbone.emulateJSON = true</code>
<br />
If you're working with a legacy web server that can't handle requests
encoded as <tt>application/json</tt>, setting <tt>Backbone.emulateJSON = true;</tt>
will cause the JSON to be serialized under a <tt>model</tt> parameter, and
the request to be made with a <tt>application/x-www-form-urlencoded</tt>
MIME type, as if from an HTML form.
</p>
<h2 id="View">Backbone.View</h2>
<p>
Backbone views are almost more convention than they are code &mdash; they
don't determine anything about your HTML or CSS for you, and can be used
with any JavaScript templating library.
The general idea is to organize your interface into logical views,
backed by models, each of which can be updated independently when the
model changes, without having to redraw the page. Instead of digging into
a JSON object, looking up an element in the DOM, and updating the HTML by hand,
you can bind your view's <tt>render</tt> function to the model's <tt>"change"</tt>
event &mdash; and now everywhere that
model data is displayed in the UI, it is always immediately up to date.
</p>
<p id="View-extend">
<b class="header">extend</b><code>Backbone.View.extend(properties, [classProperties])</code>
<br />
Get started with views by creating a custom view class. You'll want to
override the <a href="#View-render">render</a> function, specify your
declarative <a href="#View-delegateEvents">events</a>, and perhaps the
<tt>tagName</tt>, <tt>className</tt>, or <tt>id</tt> of the View's root
element.
</p>
<pre>
var DocumentRow = Backbone.View.extend({
tagName: "li",
className: "document-row",
events: {
"click .icon": "open",
"click .button.edit": "openEditDialog",
"click .button.delete": "destroy"
},
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function() {
...
}
});
</pre>
<p>
Properties like <tt>tagName</tt>, <tt>id</tt>, <tt>className</tt>,
<tt>el</tt>, and <tt>events</tt> may also be defined as a function, if
you want to wait to define them until runtime.
</p>
<p id="View-constructor">
<b class="header">constructor / initialize</b><code>new View([options])</code>
<br />
There are several special
options that, if passed, will be attached directly to the view:
<tt>model</tt>, <tt>collection</tt>,
<tt>el</tt>, <tt>id</tt>, <tt>className</tt>, <tt>tagName</tt>, <tt>attributes</tt> and <tt>events</tt>.
If the view defines an <b>initialize</b> function, it will be called when
the view is first created. If you'd like to create a view that references
an element <i>already</i> in the DOM, pass in the element as an option:
<tt>new View({el: existingElement})</tt>
</p>
<pre>
var doc = documents.first();
new DocumentRow({
model: doc,
id: "document-row-" + doc.id
});
</pre>
<p id="View-el">
<b class="header">el</b><code>view.el</code>
<br />
All views have a DOM element at all times (the <b>el</b> property),
whether they've already been inserted into the page or not. In this
fashion, views can be rendered at any time, and inserted into the DOM all
at once, in order to get high-performance UI rendering with as few
reflows and repaints as possible.
</p>
<p>
<tt>this.el</tt> can be resolved from a DOM selector string or an Element;
otherwise it will be created from the view's <tt>tagName</tt>, <tt>className</tt>,
<tt>id</tt> and <a href="#View-attributes"><tt>attributes</tt></a> properties.
If none are set, <tt>this.el</tt> is an empty <tt>div</tt>, which is often just
fine. An <b>el</b> reference may also be passed in to the view's constructor.
</p>
<pre class="runnable">
var ItemView = Backbone.View.extend({
tagName: 'li'
});
var BodyView = Backbone.View.extend({
el: 'body'
});
var item = new ItemView();
var body = new BodyView();
alert(item.el + ' ' + body.el);
</pre>
<p id="View-$el">
<b class="header">$el</b><code>view.$el</code>
<br />
A cached jQuery object for the view's element. A handy
reference instead of re-wrapping the DOM element all the time.
</p>
<pre>
view.$el.show();
listView.$el.append(itemView.el);
</pre>
<p id="View-setElement">
<b class="header">setElement</b><code>view.setElement(element)</code>
<br />
If you'd like to apply a Backbone view to a different DOM element, use
<b>setElement</b>, which will also create the cached <tt>$el</tt> reference
and move the view's delegated events from the old element to the new one.
</p>
<p id="View-attributes">
<b class="header">attributes</b><code>view.attributes</code>
<br />
A hash of attributes that will be set as HTML DOM element attributes on the
view's <tt>el</tt> (id, class, data-properties, etc.), or a function that
returns such a hash.
</p>
<p id="View-dollar">
<b class="header">$ (jQuery)</b><code>view.$(selector)</code>
<br />
If jQuery is included on the page, each view has a
<b>$</b> function that runs queries scoped within the view's element. If you use this
scoped jQuery function, you don't have to use model ids as part of your query
to pull out specific elements in a list, and can rely much more on HTML class
attributes. It's equivalent to running: <tt>view.$el.find(selector)</tt>
</p>
<pre>
ui.Chapter = Backbone.View.extend({
serialize : function() {
return {
title: this.$(".title").text(),
start: this.$(".start-page").text(),
end: this.$(".end-page").text()
};
}
});
</pre>
<p id="View-template">
<b class="header">template</b><code>view.template([data])</code>
<br />
While templating for a view isn't a function provided directly by Backbone,
it's often a nice convention to define a <b>template</b> function on your
views. In this way, when rendering your view, you have convenient access to
instance data.
For example, using Underscore templates:
</p>
<pre>
var LibraryView = Backbone.View.extend({
template: _.template(...)
});
</pre>
<p id="View-render">
<b class="header">render</b><code>view.render()</code>
<br />
The default implementation of <b>render</b> is a no-op. Override this
function with your code that renders the view template from model data,
and updates <tt>this.el</tt> with the new HTML. A good
convention is to <tt>return this</tt> at the end of <b>render</b> to
enable chained calls.
</p>
<pre>
var Bookmark = Backbone.View.extend({
template: _.template(...),
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
}
});
</pre>
<p>
Backbone is agnostic with respect to your preferred method of HTML templating.
Your <b>render</b> function could even munge together an HTML string, or use
<tt>document.createElement</tt> to generate a DOM tree. However, we suggest
choosing a nice JavaScript templating library.
<a href="http://github.com/janl/mustache.js">Mustache.js</a>,
<a href="http://github.com/creationix/haml-js">Haml-js</a>, and
<a href="http://github.com/sstephenson/eco">Eco</a> are all fine alternatives.
Because <a href="http://underscorejs.org/">Underscore.js</a> is already on the page,
<a href="http://underscorejs.org/#template">_.template</a>
is available, and is an excellent choice if you prefer simple
interpolated-JavaScript style templates.
</p>
<p>
Whatever templating strategy you end up with, it's nice if you <i>never</i>
have to put strings of HTML in your JavaScript. At DocumentCloud, we
use <a href="http://documentcloud.github.com/jammit/">Jammit</a> in order
to package up JavaScript templates stored in <tt>/app/views</tt> as part
of our main <tt>core.js</tt> asset package.
</p>
<p id="View-remove">
<b class="header">remove</b><code>view.remove()</code>
<br />
Removes a view and its <tt>el</tt> from the DOM, and calls
<a href="#Events-stopListening">stopListening</a> to remove any bound
events that the view has <a href="#Events-listenTo">listenTo</a>'d.
</p>
<p id="View-events">
<b class="header">events</b><code>view.events or view.events()</code>
<br />
The <b>events</b> hash (or method) can be used to specify a set of DOM
events that will be bound to methods on your View
through <a href="#View-delegateEvents">delegateEvents</a>.
</p>
<p>
Backbone will automatically attach the event listeners at instantiation
time, right before invoking <a href="#View-constructor">initialize</a>.
</p>
<pre>
var ENTER_KEY = 13;
var InputView = Backbone.View.extend({
tagName: 'input',
events: {
"keydown" : "keyAction",
},
render: function() { ... },
keyAction: function(e) {
if (e.which === ENTER_KEY) {
this.collection.add({text: this.$el.val()});
}
}
});
</pre>
<p id="View-delegateEvents">
<b class="header">delegateEvents</b><code>delegateEvents([events])</code>
<br />
Uses jQuery's <tt>on</tt> function to provide declarative callbacks
for DOM events within a view.
If an <b>events</b> hash is not passed directly, uses <tt>this.events</tt>
as the source. Events are written in the format <tt>{"event selector": "callback"}</tt>.
The callback may be either the name of a method on the view, or a direct
function body.
Omitting the <tt>selector</tt> causes the event to be bound to the view's
root element (<tt>this.el</tt>). By default, <tt>delegateEvents</tt> is called
within the View's constructor for you, so if you have a simple <tt>events</tt>
hash, all of your DOM events will always already be connected, and you will
never have to call this function yourself.
</p>
<p>
The <tt>events</tt> property may also be defined as a function that returns
an <b>events</b> hash, to make it easier to programmatically define your
events, as well as inherit them from parent views.
</p>
<p>
Using <b>delegateEvents</b> provides a number of advantages over manually
using jQuery to bind events to child elements during <a href="#View-render">render</a>. All attached
callbacks are bound to the view before being handed off to jQuery, so when
the callbacks are invoked, <tt>this</tt> continues to refer to the view object. When
<b>delegateEvents</b> is run again, perhaps with a different <tt>events</tt>
hash, all callbacks are removed and delegated afresh &mdash; useful for
views which need to behave differently when in different modes.
</p>
<p>
A single-event version of <b>delegateEvents</b> is available as <tt>delegate</tt>.
In fact, <b>delegateEvents</b> is simply a multi-event wrapper around <tt>delegate</tt>.
A counterpart to <tt>undelegateEvents</tt> is available as <tt>undelegate</tt>.
</p>
<p>
A view that displays a document in a search result might look
something like this:
</p>
<pre>
var DocumentView = Backbone.View.extend({
events: {
"dblclick" : "open",
"click .icon.doc" : "select",
"contextmenu .icon.doc" : "showMenu",
"click .show_notes" : "toggleNotes",
"click .title .lock" : "editAccessLevel",
"mouseover .title .date" : "showTooltip"
},
render: function() {
this.$el.html(this.template(this.model.attributes));
return this;
},
open: function() {
window.open(this.model.get("viewer_url"));
},
select: function() {
this.model.set({selected: true});
},
...
});
</pre>
<p id="View-undelegateEvents">
<b class="header">undelegateEvents</b><code>undelegateEvents()</code>
<br />
Removes all of the view's delegated events. Useful if you want to disable
or remove a view from the DOM temporarily.
</p>
<h2 id="Utility">Utility</h2>
<p id="Utility-Backbone-noConflict">
<b class="header">Backbone.noConflict</b><code>var backbone = Backbone.noConflict();</code>
<br />
Returns the <tt>Backbone</tt> object back to its original value. You can
use the return value of <tt>Backbone.noConflict()</tt> to keep a local
reference to Backbone. Useful for embedding Backbone on third-party
websites, where you don't want to clobber the existing Backbone.
</p>
<pre>
var localBackbone = Backbone.noConflict();
var model = localBackbone.Model.extend(...);
</pre>
<p id="Utility-Backbone-$">
<b class="header">Backbone.$</b><code>Backbone.$ = $;</code>
<br />
If you have multiple copies of <tt>jQuery</tt> on the page, or simply want
to tell Backbone to use a particular object as its DOM / Ajax library,
this is the property for you.
</p>
<pre>
Backbone.$ = require('jquery');
</pre>
<h2 id="faq">F.A.Q.</h2>
<p id="FAQ-why-backbone">
<b class="header">Why use Backbone, not [other framework X]?</b>
<br />
If your eye hasn't already been caught by the adaptability and elan on display
in the above <a href="#examples">list of examples</a>, we can get more specific:
Backbone.js aims to provide the common foundation that data-rich web applications
with ambitious interfaces require &mdash; while very deliberately avoiding
painting you into a corner by making any decisions that you're
better equipped to make yourself.
</p>
<ul>
<li>
The focus is on supplying you with
<a href="#Collection-Underscore-Methods">helpful methods to manipulate and
query your data</a>, not on HTML widgets or reinventing the JavaScript
object model.
</li>
<li>
Backbone does not force you to use a single template engine. Views can bind
to HTML constructed in
<a href="http://underscorejs.org/#template">your</a>
<a href="http://guides.rubyonrails.org/layouts_and_rendering.html">favorite</a>
<a href="http://mustache.github.com">way</a>.
</li>
<li>
It's smaller. There are fewer kilobytes for your browser or phone to download,
and less <i>conceptual</i> surface area. You can read and understand
the source in an afternoon.
</li>
<li>
It doesn't depend on stuffing application logic into your HTML.
There's no embedded JavaScript, template logic, or binding hookup code in
<tt>data-</tt> or <tt>ng-</tt> attributes, and no need to invent your own HTML tags.
</li>
<li>
<a href="#Events">Synchronous events</a> are used as the fundamental
building block, not a difficult-to-reason-about run loop, or by constantly
polling and traversing your data structures to hunt for changes. And if
you want a specific event to be asynchronous and aggregated,
<a href="http://underscorejs.org/#debounce">no problem</a>.
</li>
<li>
Backbone scales well, from <a href="http://disqus.com">embedded widgets</a>
to <a href="http://www.usatoday.com">massive apps</a>.
</li>
<li>
Backbone is a library, not a framework, and plays well with others.
You can embed Backbone widgets in Dojo apps without trouble, or use Backbone
models as the data backing for D3 visualizations (to pick two entirely
random examples).
</li>
<li>
"Two-way data-binding" is avoided. While it certainly makes for a nifty
demo, and works for the most basic CRUD, it doesn't tend to be terribly
useful in your real-world app. Sometimes you want to update on
every keypress, sometimes on blur, sometimes when the panel is closed,
and sometimes when the "save" button is clicked. In almost all cases, simply
serializing the form to JSON is faster and easier. All that aside, if your
heart is set, <a href="http://rivetsjs.com">go</a>
<a href="http://nytimes.github.com/backbone.stickit/">for it</a>.
</li>
<li