PageRenderTime 92ms CodeModel.GetById 4ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/index.html

https://github.com/jetleigh/backbone
HTML | 3036 lines | 2597 code | 439 blank | 0 comment | 0 complexity | c41c0c28aa6506b7e5577a2ac199b36a MD5 | raw file
   1<!DOCTYPE HTML>
   2<html>
   3<head>
   4  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
   5  <meta http-equiv="X-UA-Compatible" content="chrome=1">
   6  <title>Backbone.js</title>
   7  <style>
   8    body {
   9      font-size: 14px;
  10      line-height: 22px;
  11      font-family: Helvetica Neue, Helvetica, Arial;
  12      background: #f4f4f4 url(docs/images/background.png);
  13    }
  14    .interface {
  15      font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
  16    }
  17    div#sidebar {
  18      background: #fff;
  19      position: fixed;
  20      top: 0; left: 0; bottom: 0;
  21      width: 200px;
  22      overflow-y: auto;
  23      overflow-x: hidden;
  24      padding: 15px 0 30px 30px;
  25      border-right: 1px solid #bbb;
  26      box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
  27    }
  28      a.toc_title, a.toc_title:visited {
  29        display: block;
  30        color: black;
  31        font-weight: bold;
  32        margin-top: 15px;
  33      }
  34        a.toc_title:hover {
  35          text-decoration: underline;
  36        }
  37        #sidebar .version {
  38          font-size: 10px;
  39          font-weight: normal;
  40        }
  41      ul.toc_section {
  42        font-size: 11px;
  43        line-height: 14px;
  44        margin: 5px 0 0 0;
  45        padding-left: 0px;
  46        list-style-type: none;
  47        font-family: Lucida Grande;
  48      }
  49        .toc_section li {
  50          cursor: pointer;
  51          margin: 0 0 3px 0;
  52        }
  53          .toc_section li a {
  54            text-decoration: none;
  55            color: black;
  56          }
  57            .toc_section li a:hover {
  58              text-decoration: underline;
  59            }
  60    div.container {
  61      position: relative;
  62      width: 550px;
  63      margin: 40px 0 50px 260px;
  64    }
  65    div.run {
  66      position: absolute;
  67      right: 15px;
  68      width: 26px; height: 18px;
  69      background: url('docs/images/arrows.png') no-repeat -26px 0;
  70    }
  71      div.run:active {
  72        background-position: -51px 0;
  73      }
  74    p, div.container ul {
  75      margin: 20px 0;
  76      width: 550px;
  77    }
  78      p.warning {
  79        font-size: 12px;
  80        line-height: 18px;
  81        font-style: italic;
  82      }
  83      div.container ul {
  84        list-style: circle;
  85        font-size: 12px;
  86        padding-left: 15px;
  87      }
  88    a, a:visited {
  89      color: #444;
  90    }
  91    a:active, a:hover {
  92      color: #000;
  93    }
  94    a img {
  95      border: 0;
  96    }
  97    h1, h2, h3, h4, h5, h6 {
  98      padding-top: 20px;
  99    }
 100      h2 {
 101        font-size: 20px;
 102      }
 103    b.header {
 104      font-size: 16px;
 105      line-height: 30px;
 106    }
 107    span.alias {
 108      font-size: 14px;
 109      font-style: italic;
 110      margin-left: 20px;
 111    }
 112    table {
 113      margin: 15px 0 0; padding: 0;
 114    }
 115      tr, td {
 116        margin: 0; padding: 0;
 117      }
 118        td {
 119          padding: 0px 15px 5px 0;
 120        }
 121    code, pre, tt {
 122      font-family: Monaco, Consolas, "Lucida Console", monospace;
 123      font-size: 12px;
 124      line-height: 18px;
 125      font-style: normal;
 126    }
 127      tt {
 128        padding: 0px 3px;
 129        background: #fff;
 130        border: 1px solid #ddd;
 131        zoom: 1;
 132      }
 133      code {
 134        margin-left: 20px;
 135      }
 136      pre {
 137        font-size: 12px;
 138        padding: 2px 0 2px 15px;
 139        border: 4px solid #bbb; border-top: 0; border-bottom: 0;
 140        margin: 0px 0 30px;
 141      }
 142      img.example_image {
 143        margin: 0px auto;
 144      }
 145  </style>
 146</head>
 147<body>
 148
 149  <div id="sidebar" class="interface">
 150
 151    <a class="toc_title" href="#">
 152      Backbone.js <span class="version">(0.5.3)</span>
 153    </a>
 154
 155    <a class="toc_title" href="#Introduction">
 156      Introduction
 157    </a>
 158
 159    <a class="toc_title" href="#Events">
 160      Events
 161    </a>
 162    <ul class="toc_section">
 163      <li><a href="#Events-bind">bind</a></li>
 164      <li><a href="#Events-unbind">unbind</a></li>
 165      <li><a href="#Events-trigger">trigger</a></li>
 166    </ul>
 167
 168    <a class="toc_title" href="#Model">
 169      Model
 170    </a>
 171    <ul class="toc_section">
 172      <li><a href="#Model-extend">extend</a></li>
 173      <li><a href="#Model-constructor">constructor / initialize</a></li>
 174      <li><a href="#Model-get">get</a></li>
 175      <li><a href="#Model-set">set</a></li>
 176      <li><a href="#Model-escape">escape</a></li>
 177      <li><a href="#Model-has">has</a></li>
 178      <li><a href="#Model-unset">unset</a></li>
 179      <li><a href="#Model-clear">clear</a></li>
 180      <li><a href="#Model-id">id</a></li>
 181      <li><a href="#Model-idAttribute">idAttribute</a></li>
 182      <li><a href="#Model-cid">cid</a></li>
 183      <li><a href="#Model-attributes">attributes</a></li>
 184      <li><a href="#Model-defaults">defaults</a></li>
 185      <li>- <a href="#Model-toJSON">toJSON</a></li>
 186      <li><a href="#Model-fetch">fetch</a></li>
 187      <li><a href="#Model-save">save</a></li>
 188      <li><a href="#Model-destroy">destroy</a></li>
 189      <li><a href="#Model-validate">validate</a></li>
 190      <li><a href="#Model-url">url</a></li>
 191      <li><a href="#Model-urlRoot">urlRoot</a></li>
 192      <li><a href="#Model-parse">parse</a></li>
 193      <li><a href="#Model-clone">clone</a></li>
 194      <li><a href="#Model-isNew">isNew</a></li>
 195      <li><a href="#Model-change">change</a></li>
 196      <li><a href="#Model-hasChanged">hasChanged</a></li>
 197      <li><a href="#Model-changedAttributes">changedAttributes</a></li>
 198      <li><a href="#Model-previous">previous</a></li>
 199      <li><a href="#Model-previousAttributes">previousAttributes</a></li>
 200    </ul>
 201
 202    <a class="toc_title" href="#Collection">
 203      Collection
 204    </a>
 205    <ul class="toc_section">
 206      <li><a href="#Collection-extend">extend</a></li>
 207      <li><a href="#Collection-model">model</a></li>
 208      <li><a href="#Collection-constructor">constructor / initialize</a></li>
 209      <li><a href="#Collection-models">models</a></li>
 210      <li><a href="#Collection-toJSON">toJSON</a></li>
 211      <li><a href="#Collection-Underscore-Methods"><b>Underscore Methods (26)</b></a></li>
 212      <li><a href="#Collection-add">add</a></li>
 213      <li><a href="#Collection-remove">remove</a></li>
 214      <li><a href="#Collection-get">get</a></li>
 215      <li><a href="#Collection-getByCid">getByCid</a></li>
 216      <li><a href="#Collection-at">at</a></li>
 217      <li><a href="#Collection-length">length</a></li>
 218      <li><a href="#Collection-comparator">comparator</a></li>
 219      <li><a href="#Collection-sort">sort</a></li>
 220      <li><a href="#Collection-pluck">pluck</a></li>
 221      <li><a href="#Collection-url">url</a></li>
 222      <li><a href="#Collection-parse">parse</a></li>
 223      <li><a href="#Collection-fetch">fetch</a></li>
 224      <li><a href="#Collection-reset">reset</a></li>
 225      <li><a href="#Collection-create">create</a></li>
 226    </ul>
 227
 228    <a class="toc_title" href="#Router">
 229      Router
 230    </a>
 231    <ul class="toc_section">
 232      <li><a href="#Router-extend">extend</a></li>
 233      <li><a href="#Router-routes">routes</a></li>
 234      <li><a href="#Router-constructor">constructor / initialize</a></li>
 235      <li><a href="#Router-route">route</a></li>
 236      <li><a href="#Router-navigate">navigate</a></li>
 237    </ul>
 238
 239    <a class="toc_title" href="#History">
 240      History
 241    </a>
 242    <ul class="toc_section">
 243      <li><a href="#History-start">start</a></li>
 244    </ul>
 245
 246    <a class="toc_title" href="#Sync">
 247      Sync
 248    </a>
 249    <ul class="toc_section">
 250      <li><a href="#Sync">Backbone.sync</a></li>
 251      <li><a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
 252      <li><a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
 253    </ul>
 254
 255    <a class="toc_title" href="#View">
 256      View
 257    </a>
 258    <ul class="toc_section">
 259      <li><a href="#View-extend">extend</a></li>
 260      <li><a href="#View-constructor">constructor / initialize</a></li>
 261      <li><a href="#View-el">el</a></li>
 262      <li><a href="#View-dollar">$ (jQuery or Zepto)</a></li>
 263      <li><a href="#View-render">render</a></li>
 264      <li><a href="#View-remove">remove</a></li>
 265      <li><a href="#View-make">make</a></li>
 266      <li><a href="#View-delegateEvents">delegateEvents</a></li>
 267    </ul>
 268
 269    <a class="toc_title" href="#Utility">
 270      Utility
 271    </a>
 272    <ul class="toc_section">
 273      <li><a href="#Utility-noConflict">noConflict</a></li>
 274    </ul>
 275
 276    <a class="toc_title" href="#examples">
 277      Examples
 278    </a>
 279    <ul class="toc_section">
 280      <li><a href="#examples-todos">Todos</a></li>
 281      <li><a href="#examples-documentcloud">DocumentCloud</a></li>
 282      <li><a href="#examples-linkedin">LinkedIn Mobile</a></li>
 283      <li><a href="#examples-flow">Flow</a></li>
 284      <li><a href="#examples-audiovroom">AudioVroom</a></li>
 285      <li><a href="#examples-foursquare">Foursquare</a></li>
 286      <li><a href="#examples-do">Do</a></li>
 287      <li><a href="#examples-posterous">Posterous Spaces</a></li>
 288      <li><a href="#examples-groupon">Groupon Now!</a></li>
 289      <li><a href="#examples-basecamp">Basecamp Mobile</a></li>
 290      <li><a href="#examples-slavery-footprint">Slavery Footprint</a></li>
 291      <li><a href="#examples-diaspora">Diaspora</a></li>
 292      <li><a href="#examples-trajectory">Trajectory</a></li>
 293      <li><a href="#examples-soundcloud">SoundCloud Mobile</a></li>
 294      <li><a href="#examples-pandora">Pandora</a></li>
 295      <li><a href="#examples-cloudapp">CloudApp</a></li>
 296      <li><a href="#examples-seatgeek">SeatGeek</a></li>
 297      <li><a href="#examples-grove">Grove.io</a></li>
 298      <li><a href="#examples-kicksend">Kicksend</a></li>
 299      <li><a href="#examples-shortmail">Shortmail</a></li>
 300      <li><a href="#examples-battlefield">Battlefield Play4Free</a></li>
 301      <li><a href="#examples-salon">Salon.io</a></li>
 302      <li><a href="#examples-quoteroller">Quote Roller</a></li>
 303      <li><a href="#examples-tilemill">TileMill</a></li>
 304      <li><a href="#examples-rround">rround.me</a></li>
 305      <li>- <a href="#examples-blossom">Blossom</a></li>
 306      <li>- <a href="#examples-instagreat">Insta-great!</a></li>
 307      <li>- <a href="#examples-decide">Decide</a></li>
 308      <li>- <a href="#examples-trello">Trello</a></li>
 309      <li>- <a href="#examples-bittorrent">BitTorrent</a></li>
 310      <li>- <a href="#examples-picplum">Picplum</a></li>
 311      <li>- <a href="#examples-ducksboard">Ducksboard</a></li>
 312      <li>- <a href="#examples-quietwrite">QuietWrite</a></li>
 313      <li>- <a href="#examples-tzigla">Tzigla</a></li>
 314    </ul>
 315
 316    <a class="toc_title" href="#faq">
 317      F.A.Q.
 318    </a>
 319    <ul class="toc_section">
 320      <li><a href="#FAQ-events">Catalog of Events</a></li>
 321      <li><a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
 322      <li><a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
 323      <li><a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
 324      <li><a href="#FAQ-mvc">Traditional MVC</a></li>
 325      <li><a href="#FAQ-this">Binding "this"</a></li>
 326    </ul>
 327
 328    <a class="toc_title" href="#changelog">
 329      Change Log
 330    </a>
 331
 332  </div>
 333
 334  <div class="container">
 335
 336    <p>
 337      <img style="width: 451px; height: 80px;" src="docs/images/backbone.png" alt="Backbone.js" />
 338    </p>
 339
 340    <p>
 341      <a href="http://github.com/documentcloud/backbone/">Backbone</a>
 342      supplies structure to JavaScript-heavy applications by providing <b>models</b> with
 343      key-value binding and custom events, <b>collections</b> with a rich API of enumerable functions,
 344      <b>views</b> with declarative event handling, and connects it all to your
 345      existing application over a RESTful JSON interface.
 346    </p>
 347
 348    <p>
 349      The project is <a href="http://github.com/documentcloud/backbone/">hosted on GitHub</a>,
 350      and the <a href="docs/backbone.html">annotated source code</a> is available,
 351      as well as an online <a href="test/test.html">test suite</a>, an
 352      <a href="examples/todos/index.html">example application</a> and a
 353      <a href="https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>.
 354    </p>
 355
 356    <p>
 357      You can report bugs and discuss features on the
 358      <a href="http://github.com/documentcloud/backbone/issues">GitHub issues page</a>,
 359      on Freenode IRC in the <tt>#documentcloud</tt> channel, post questions to the
 360      <a href="https://groups.google.com/forum/#!forum/backbonejs">Google Group</a>,
 361      or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
 362    </p>
 363
 364    <p>
 365      <i>
 366        Backbone is an open-source component of
 367        <a href="http://documentcloud.org/">DocumentCloud</a>.
 368      </i>
 369    </p>
 370
 371    <h2 id="downloads">
 372      Downloads &amp; Dependencies
 373      <span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
 374    </h2>
 375
 376    <table>
 377      <tr>
 378        <td><a href="backbone.js">Development Version (0.5.3)</a></td>
 379        <td><i>41kb, Full Source with Comments</i></td>
 380      </tr>
 381      <tr>
 382        <td><a href="backbone-min.js">Production Version (0.5.3)</a></td>
 383        <td><i>4.6kb, Packed and Gzipped</i></td>
 384      </tr>
 385    </table>
 386
 387    <p>
 388      Backbone's only hard dependency is
 389      <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>.
 390      For RESTful persistence, history support via <a href="#Router">Backbone.Router</a>
 391      and DOM manipulation with <a href="#View">Backbone.View</a>, include
 392      <a href="https://github.com/douglascrockford/JSON-js">json2.js</a>, and either
 393      <a href="http://jquery.com">jQuery</a> <small>( > 1.4.2)</small> or
 394      <a href="http://zeptojs.com/">Zepto</a>.
 395    </p>
 396
 397    <h2 id="Introduction">Introduction</h2>
 398
 399    <p>
 400      When working on a web application that involves a lot of JavaScript, one
 401      of the first things you learn is to stop tying your data to the DOM. It's all
 402      too easy to create JavaScript applications that end up as tangled piles of
 403      jQuery selectors and callbacks, all trying frantically to keep data in
 404      sync between the HTML UI, your JavaScript logic, and the database on your
 405      server. For rich client-side applications, a more structured approach
 406      is often helpful.
 407    </p>
 408
 409    <p>
 410      With Backbone, you represent your data as
 411      <a href="#Model">Models</a>, which can be created, validated, destroyed,
 412      and saved to the server. Whenever a UI action causes an attribute of
 413      a model to change, the model triggers a <i>"change"</i> event; all
 414      the <a href="#View">Views</a> that display the model's state can be notified of the
 415      change, so that they are able to respond accordingly, re-rendering themselves with
 416      the new information. In a finished Backbone app, you don't have to write the glue
 417      code that looks into the DOM to find an element with a specific <i>id</i>,
 418      and update the HTML manually
 419      &mdash; when the model changes, the views simply update themselves.
 420    </p>
 421
 422    <p>
 423      Many of the examples that follow are runnable. Click the <i>play</i> button
 424      to execute them.
 425    </p>
 426
 427    <h2 id="Events">Backbone.Events</h2>
 428
 429    <p>
 430      <b>Events</b> is a module that can be mixed in to any object, giving the
 431      object the ability to bind and trigger custom named events. Events do not
 432      have to be declared before they are bound, and may take passed arguments.
 433      For example:
 434    </p>
 435
 436<pre class="runnable">
 437var object = {};
 438
 439_.extend(object, Backbone.Events);
 440
 441object.bind("alert", function(msg) {
 442  alert("Triggered " + msg);
 443});
 444
 445object.trigger("alert", "an event");
 446</pre>
 447
 448    <p id="Events-bind">
 449      <b class="header">bind</b><code>object.bind(event, callback, [context])</code>
 450      <br />
 451      Bind a <b>callback</b> function to an object. The callback will be invoked
 452      whenever the <b>event</b> (specified by an arbitrary string identifier) is fired.
 453      If you have a large number of different events on a page, the convention is to use colons to
 454      namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>
 455    </p>
 456
 457    <p>
 458      To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
 459      pass the optional third argument: <tt>model.bind('change', this.render, this)</tt>
 460    </p>
 461
 462    <p>
 463      Callbacks bound to the special
 464      <tt>"all"</tt> event will be triggered when any event occurs, and are passed
 465      the name of the event as the first argument. For example, to proxy all events
 466      from one object to another:
 467    </p>
 468
 469<pre>
 470proxy.bind("all", function(eventName) {
 471  object.trigger(eventName);
 472});
 473</pre>
 474
 475    <p id="Events-unbind">
 476      <b class="header">unbind</b><code>object.unbind([event], [callback])</code>
 477      <br />
 478      Remove a previously-bound <b>callback</b> function from an object. If no
 479      callback is specified, all callbacks for the <b>event</b> will be
 480      removed. If no event is specified, <i>all</i> event callbacks on the object
 481      will be removed.
 482    </p>
 483
 484<pre>
 485object.unbind("change", onChange);  // Removes just the onChange callback.
 486
 487object.unbind("change");            // Removes all "change" callbacks.
 488
 489object.unbind();                    // Removes all callbacks on object.
 490</pre>
 491
 492    <p id="Events-trigger">
 493      <b class="header">trigger</b><code>object.trigger(event, [*args])</code>
 494      <br />
 495      Trigger callbacks for the given <b>event</b>. Subsequent arguments to
 496      <b>trigger</b> will be passed along to the event callbacks.
 497    </p>
 498
 499    <h2 id="Model">Backbone.Model</h2>
 500
 501    <p>
 502      <b>Models</b> are the heart of any JavaScript application, containing
 503      the interactive data as well as a large part of the logic surrounding it:
 504      conversions, validations, computed properties, and access control. You
 505      extend <b>Backbone.Model</b> with your domain-specific methods, and
 506      <b>Model</b> provides a basic set of functionality for managing changes.
 507    </p>
 508
 509    <p>
 510      The following is a contrived example, but it demonstrates defining a model
 511      with a custom method, setting an attribute, and firing an event keyed
 512      to changes in that specific attribute.
 513      After running this code once, <tt>sidebar</tt> will be
 514      available in your browser's console, so you can play around with it.
 515    </p>
 516
 517<pre class="runnable">
 518var Sidebar = Backbone.Model.extend({
 519  promptColor: function() {
 520    var cssColor = prompt("Please enter a CSS color:");
 521    this.set({color: cssColor});
 522  }
 523});
 524
 525window.sidebar = new Sidebar;
 526
 527sidebar.bind('change:color', function(model, color) {
 528  $('#sidebar').css({background: color});
 529});
 530
 531sidebar.set({color: 'white'});
 532
 533sidebar.promptColor();
 534</pre>
 535
 536    <p id="Model-extend">
 537      <b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
 538      <br />
 539      To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
 540      and provide instance <b>properties</b>, as well as optional
 541      <b>classProperties</b> to be attached directly to the constructor function.
 542    </p>
 543
 544    <p>
 545      <b>extend</b> correctly sets up the prototype chain, so subclasses created
 546      with <b>extend</b> can be further extended and subclassed as far as you like.
 547    </p>
 548
 549<pre>
 550var Note = Backbone.Model.extend({
 551
 552  initialize: function() { ... },
 553
 554  author: function() { ... },
 555
 556  coordinates: function() { ... },
 557
 558  allowedToEdit: function(account) {
 559    return true;
 560  }
 561
 562});
 563
 564var PrivateNote = Note.extend({
 565
 566  allowedToEdit: function(account) {
 567    return account.owns(this);
 568  }
 569
 570});
 571</pre>
 572
 573    <p class="warning">
 574        Brief aside on <tt>super</tt>: JavaScript does not provide
 575        a simple way to call super &mdash; the function of the same name defined
 576        higher on the prototype chain. If you override a core function like
 577        <tt>set</tt>, or <tt>save</tt>, and you want to invoke the
 578        parent object's implementation, you'll have to explicitly call it, along these lines:
 579    </p>
 580
 581<pre>
 582var Note = Backbone.Model.extend({
 583  set: function(attributes, options) {
 584    Backbone.Model.prototype.set.call(this, attributes, options);
 585    ...
 586  }
 587});
 588</pre>
 589
 590    <p id="Model-constructor">
 591      <b class="header">constructor / initialize</b><code>new Model([attributes])</code>
 592      <br />
 593      When creating an instance of a model, you can pass in the initial values
 594      of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
 595      model. If you define an <b>initialize</b> function, it will be invoked when
 596      the model is created.
 597    </p>
 598
 599<pre>
 600new Book({
 601  title: "One Thousand and One Nights",
 602  author: "Scheherazade"
 603});
 604</pre>
 605
 606    <p id="Model-get">
 607      <b class="header">get</b><code>model.get(attribute)</code>
 608      <br />
 609      Get the current value of an attribute from the model. For example:
 610      <tt>note.get("title")</tt>
 611    </p>
 612
 613    <p id="Model-set">
 614      <b class="header">set</b><code>model.set(attributes, [options])</code>
 615      <br />
 616      Set a hash of attributes (one or many) on the model. If any of the attributes
 617      change the models state, a <tt>"change"</tt> event will be triggered, unless
 618      <tt>{silent: true}</tt> is passed as an option. Change events for specific
 619      attributes are also triggered, and you can bind to those as well, for example:
 620      <tt>change:title</tt>, and <tt>change:content</tt>.
 621    </p>
 622
 623<pre>
 624note.set({title: "October 12", content: "Lorem Ipsum Dolor Sit Amet..."});
 625</pre>
 626
 627    <p>
 628      If the model has a <a href="#Model-validate">validate</a> method,
 629      it will be validated before the attributes are set, no changes will
 630      occur if the validation fails, and <b>set</b> will return <tt>false</tt>.
 631      Otherwise, <b>set</b> returns a reference to the model.
 632      You may also pass an <tt>error</tt>
 633      callback in the options, which will be invoked instead of triggering an
 634      <tt>"error"</tt> event, should validation fail.
 635    </p>
 636
 637    <p id="Model-escape">
 638      <b class="header">escape</b><code>model.escape(attribute)</code>
 639      <br />
 640      Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
 641      of a model's attribute. If you're interpolating data from the model into
 642      HTML, using <b>escape</b> to retrieve attributes will prevent
 643      <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
 644    </p>
 645
 646<pre class="runnable">
 647var hacker = new Backbone.Model({
 648  name: "&lt;script&gt;alert('xss')&lt;/script&gt;"
 649});
 650
 651alert(hacker.escape('name'));
 652</pre>
 653
 654    <p id="Model-has">
 655      <b class="header">has</b><code>model.has(attribute)</code>
 656      <br />
 657      Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
 658      value.
 659    </p>
 660
 661<pre>
 662if (note.has("title")) {
 663  ...
 664}
 665</pre>
 666
 667    <p id="Model-unset">
 668      <b class="header">unset</b><code>model.unset(attribute, [options])</code>
 669      <br />
 670      Remove an attribute by deleting it from the internal attributes hash.
 671      Fires a <tt>"change"</tt> event unless <tt>silent</tt> is passed as an option.
 672    </p>
 673
 674    <p id="Model-clear">
 675      <b class="header">clear</b><code>model.clear([options])</code>
 676      <br />
 677      Removes all attributes from the model. Fires a <tt>"change"</tt> event unless
 678      <tt>silent</tt> is passed as an option.
 679    </p>
 680
 681    <p id="Model-id">
 682      <b class="header">id</b><code>model.id</code>
 683      <br />
 684      A special property of models, the <b>id</b> is an arbitrary string
 685      (integer id or UUID). If you set the <b>id</b> in the
 686      attributes hash, it will be copied onto the model as a direct property.
 687      Models can be retrieved by id from collections, and the id is used to generate
 688      model URLs by default.
 689    </p>
 690
 691    <p id="Model-idAttribute">
 692      <b class="header">idAttribute</b><code>model.idAttribute</code>
 693      <br />
 694      A model's unique identifier is stored under the <tt>id</tt> attribute.
 695      If you're directly communicating with a backend (CouchDB, MongoDB) that uses
 696      a different unique key, you may set a Model's <tt>idAttribute</tt> to
 697      transparently map from that key to <tt>id</tt>.
 698
 699<pre class="runnable">
 700var Meal = Backbone.Model.extend({
 701  idAttribute: "_id"
 702});
 703
 704var cake = new Meal({ _id: 1, name: "Cake" });
 705alert("Cake id: " + cake.id);
 706</pre>
 707    </p>
 708
 709    <p id="Model-cid">
 710      <b class="header">cid</b><code>model.cid</code>
 711      <br />
 712      A special property of models, the <b>cid</b> or client id is a unique identifier
 713      automatically assigned to all models when they're first created. Client ids
 714      are handy when the model has not yet been saved to the server, and does not
 715      yet have its eventual true <b>id</b>, but already needs to be visible in the UI.
 716      Client ids take the form: <tt>c1, c2, c3 ...</tt>
 717    </p>
 718
 719    <p id="Model-attributes">
 720      <b class="header">attributes</b><code>model.attributes</code>
 721      <br />
 722      The <b>attributes</b> property is the internal hash containing the model's
 723      state. Please use <a href="#Model-set">set</a> to update the attributes instead of modifying
 724      them directly. If you'd like to retrieve and munge a copy of the model's
 725      attributes, use <a href="#Model-toJSON">toJSON</a> instead.
 726    </p>
 727
 728    <p id="Model-defaults">
 729      <b class="header">defaults</b><code>model.defaults or model.defaults()</code>
 730      <br />
 731      The <b>defaults</b> hash (or function) can be used to specify the default
 732      attributes for your model. When creating an instance of the model,
 733      any unspecified attributes will be set to their default value.
 734    </p>
 735
 736<pre class="runnable">
 737var Meal = Backbone.Model.extend({
 738  defaults: {
 739    "appetizer":  "caesar salad",
 740    "entree":     "ravioli",
 741    "dessert":    "cheesecake"
 742  }
 743});
 744
 745alert("Dessert will be " + (new Meal).get('dessert'));
 746</pre>
 747
 748    <p class="warning">
 749      Remember that in JavaScript, objects are passed by reference, so if you
 750      include an object as a default value, it will be shared among all instances.
 751    </p>
 752
 753    <p id="Model-toJSON">
 754      <b class="header">toJSON</b><code>model.toJSON()</code>
 755      <br />
 756      Return a copy of the model's <a href="#Model-attributes">attributes</a> for JSON stringification.
 757      This can be used for persistence, serialization, or for augmentation before
 758      being handed off to a view. The name of this method is a bit confusing, as
 759      it doesn't actually return a JSON string &mdash; but I'm afraid that it's
 760      the way that the <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript API for <b>JSON.stringify</b> works</a>.
 761    </p>
 762
 763<pre class="runnable">
 764var artist = new Backbone.Model({
 765  firstName: "Wassily",
 766  lastName: "Kandinsky"
 767});
 768
 769artist.set({birthday: "December 16, 1866"});
 770
 771alert(JSON.stringify(artist));
 772</pre>
 773
 774    <p id="Model-fetch">
 775      <b class="header">fetch</b><code>model.fetch([options])</code>
 776      <br />
 777      Resets the model's state from the server. Useful if the model has never
 778      been populated with data, or if you'd like to ensure that you have the
 779      latest server state. A <tt>"change"</tt> event will be triggered if the
 780      server's state differs from the current attributes. Accepts
 781      <tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
 782      are passed <tt>(model, response)</tt> as arguments.
 783    </p>
 784
 785<pre>
 786// Poll every 10 seconds to keep the channel model up-to-date.
 787setInterval(function() {
 788  channel.fetch();
 789}, 10000);
 790</pre>
 791
 792    <p id="Model-save">
 793      <b class="header">save</b><code>model.save([attributes], [options])</code>
 794      <br />
 795      Save a model to your database (or alternative persistence layer),
 796      by delegating to <a href="#Sync">Backbone.sync</a>. The <b>attributes</b>
 797      hash (as in <a href="#Model-set">set</a>) should contain the attributes
 798      you'd like to change -- keys that aren't mentioned won't be altered.
 799      If the model has a <a href="#Model-validate">validate</a>
 800      method, and validation fails, the model will not be saved. If the model
 801      <a href="#Model-isNew">isNew</a>, the save will be a <tt>"create"</tt>
 802      (HTTP <tt>POST</tt>), if the model already
 803      exists on the server, the save will be an <tt>"update"</tt> (HTTP <tt>PUT</tt>).
 804    </p>
 805
 806    <p>
 807      In the following example, notice how our overridden version
 808      of <tt>Backbone.sync</tt> receives a <tt>"create"</tt> request
 809      the first time the model is saved and an <tt>"update"</tt>
 810      request the second time.
 811    </p>
 812
 813<pre class="runnable">
 814Backbone.sync = function(method, model) {
 815  alert(method + ": " + JSON.stringify(model));
 816  model.id = 1;
 817};
 818
 819var book = new Backbone.Model({
 820  title: "The Rough Riders",
 821  author: "Theodore Roosevelt"
 822});
 823
 824book.save();
 825
 826book.save({author: "Teddy"});
 827</pre>
 828
 829    <p>
 830      <b>save</b> accepts <tt>success</tt> and <tt>error</tt> callbacks in the
 831      options hash, which are passed <tt>(model, response)</tt> as arguments.
 832      The <tt>error</tt> callback will also be invoked if the model has a
 833      <tt>validate</tt> method, and validation fails. If a server-side
 834      validation fails, return a non-<tt>200</tt> HTTP response code, along with
 835      an error response in text or JSON.
 836    </p>
 837
 838<pre>
 839book.save({author: "F.D.R."}, {error: function(){ ... }});
 840</pre>
 841
 842    <p id="Model-destroy">
 843      <b class="header">destroy</b><code>model.destroy([options])</code>
 844      <br />
 845      Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
 846      request to <a href="#Sync">Backbone.sync</a>. Accepts
 847      <tt>success</tt> and <tt>error</tt> callbacks in the options hash.
 848      Triggers a <tt>"destroy"</tt> event on the model, which will bubble up
 849      through any collections that contain it.
 850    </p>
 851
 852<pre>
 853book.destroy({success: function(model, response) {
 854  ...
 855}});
 856</pre>
 857
 858    <p id="Model-validate">
 859      <b class="header">validate</b><code>model.validate(attributes)</code>
 860      <br />
 861      This method is left undefined, and you're encouraged to override it with
 862      your custom validation logic, if you have any that can be performed
 863      in JavaScript. <b>validate</b> is called before <tt>set</tt> and
 864      <tt>save</tt>, and is passed the attributes that are about to be updated.
 865      If the model and attributes are valid, don't return anything from <b>validate</b>;
 866      if the attributes are invalid, return an error of your choosing. It
 867      can be as simple as a string error message to be displayed, or a complete
 868      error object that describes the error programmatically. <tt>set</tt> and
 869      <tt>save</tt> will not continue if <b>validate</b> returns an error.
 870      Failed validations trigger an <tt>"error"</tt> event.
 871    </p>
 872
 873<pre class="runnable">
 874var Chapter = Backbone.Model.extend({
 875  validate: function(attrs) {
 876    if (attrs.end < attrs.start) {
 877      return "can't end before it starts";
 878    }
 879  }
 880});
 881
 882var one = new Chapter({
 883  title : "Chapter One: The Beginning"
 884});
 885
 886one.bind("error", function(model, error) {
 887  alert(model.get("title") + " " + error);
 888});
 889
 890one.set({
 891  start: 15,
 892  end:   10
 893});
 894</pre>
 895
 896    <p>
 897      <tt>"error"</tt> events are useful for providing coarse-grained error
 898      messages at the model or collection level, but if you have a specific view
 899      that can better handle the error, you may override and suppress the event
 900      by passing an <tt>error</tt> callback directly:
 901    </p>
 902
 903<pre>
 904account.set({access: "unlimited"}, {
 905  error: function(model, error) {
 906    alert(error);
 907  }
 908});
 909</pre>
 910
 911    <p id="Model-url">
 912      <b class="header">url</b><code>model.url()</code>
 913      <br />
 914      Returns the relative URL where the model's resource would be located on
 915      the server. If your models are located somewhere else, override this method
 916      with the correct logic. Generates URLs of the form: <tt>"/[collection.url]/[id]"</tt>,
 917      falling back to <tt>"/[urlRoot]/id"</tt> if the model is not part of a collection.
 918    </p>
 919
 920    <p>
 921      Delegates to <a href="#Collection-url">Collection#url</a> to generate the
 922      URL, so make sure that you have it defined, or a <a href="#Model-urlRoot">urlRoot</a>
 923      property, if all models of this class share a common root URL.
 924      A model with an id of <tt>101</tt>, stored in a
 925      <a href="#Collection">Backbone.Collection</a> with a <tt>url</tt> of <tt>"/documents/7/notes"</tt>,
 926      would have this URL: <tt>"/documents/7/notes/101"</tt>
 927    </p>
 928
 929    <p id="Model-urlRoot">
 930      <b class="header">urlRoot</b><code>model.urlRoot</code>
 931      <br />
 932      Specify a <tt>urlRoot</tt> if you're using a model outside of a collection,
 933      to enable the default <a href="#Model-url">url</a> function to generate
 934      URLs based on the model id. <tt>"/[urlRoot]/id"</tt>
 935    </p>
 936
 937<pre class="runnable">
 938var Book = Backbone.Model.extend({urlRoot : '/books'});
 939
 940var solaris = new Book({id: "1083-lem-solaris"});
 941
 942alert(solaris.url());
 943</pre>
 944
 945    <p id="Model-parse">
 946      <b class="header">parse</b><code>model.parse(response)</code>
 947      <br />
 948      <b>parse</b> is called whenever a model's data is returned by the
 949      server, in <a href="#Model-fetch">fetch</a>, and <a href="#Model-save">save</a>.
 950      The function is passed the raw <tt>response</tt> object, and should return
 951      the attributes hash to be <a href="#Model-set">set</a> on the model. The
 952      default implementation is a no-op, simply passing through the JSON response.
 953      Override this if you need to work with a preexisting API, or better namespace
 954      your responses.
 955    </p>
 956
 957    <p>
 958      If you're working with a Rails backend, you'll notice that Rails' default
 959      <tt>to_json</tt> implementation includes a model's attributes under a
 960      namespace. To disable this behavior for seamless Backbone integration, set:
 961    </p>
 962
 963<pre>
 964ActiveRecord::Base.include_root_in_json = false
 965</pre>
 966
 967    <p id="Model-clone">
 968      <b class="header">clone</b><code>model.clone()</code>
 969      <br />
 970      Returns a new instance of the model with identical attributes.
 971    </p>
 972
 973    <p id="Model-isNew">
 974      <b class="header">isNew</b><code>model.isNew()</code>
 975      <br />
 976      Has this model been saved to the server yet? If the model does not yet have
 977      an <tt>id</tt>, it is considered to be new.
 978    </p>
 979
 980    <p id="Model-change">
 981      <b class="header">change</b><code>model.change()</code>
 982      <br />
 983      Manually trigger the <tt>"change"</tt> event.
 984      If you've been passing <tt>{silent: true}</tt> to the <a href="#Model-set">set</a> function in order to
 985      aggregate rapid changes to a model, you'll want to call <tt>model.change()</tt>
 986      when you're all finished.
 987    </p>
 988
 989    <p id="Model-hasChanged">
 990      <b class="header">hasChanged</b><code>model.hasChanged([attribute])</code>
 991      <br />
 992      Has the model changed since the last <tt>"change"</tt> event? If an <b>attribute</b>
 993      is passed, returns <tt>true</tt> if that specific attribute has changed.
 994    </p>
 995
 996    <p class="warning">
 997      Note that this method, and the following change-related ones,
 998      are only useful during the course of a <tt>"change"</tt> event.
 999    </p>
1000
1001<pre>
1002book.bind("change", function() {
1003  if (book.hasChanged("title")) {
1004    ...
1005  }
1006});
1007</pre>
1008
1009    <p id="Model-changedAttributes">
1010      <b class="header">changedAttributes</b><code>model.changedAttributes([attributes])</code>
1011      <br />
1012      Retrieve a hash of only the model's attributes that have changed. Optionally,
1013      an external <b>attributes</b> hash can be passed in, returning
1014      the attributes in that hash which differ from the model. This can be used
1015      to figure out which portions of a view should be updated, or what calls
1016      need to be made to sync the changes to the server.
1017    </p>
1018
1019    <p id="Model-previous">
1020      <b class="header">previous</b><code>model.previous(attribute)</code>
1021      <br />
1022      During a <tt>"change"</tt> event, this method can be used to get the
1023      previous value of a changed attribute.
1024    </p>
1025
1026<pre class="runnable">
1027var bill = new Backbone.Model({
1028  name: "Bill Smith"
1029});
1030
1031bill.bind("change:name", function(model, name) {
1032  alert("Changed name from " + bill.previous("name") + " to " + name);
1033});
1034
1035bill.set({name : "Bill Jones"});
1036</pre>
1037
1038    <p id="Model-previousAttributes">
1039      <b class="header">previousAttributes</b><code>model.previousAttributes()</code>
1040      <br />
1041      Return a copy of the model's previous attributes. Useful for getting a
1042      diff between versions of a model, or getting back to a valid state after
1043      an error occurs.
1044    </p>
1045
1046    <h2 id="Collection">Backbone.Collection</h2>
1047
1048    <p>
1049      Collections are ordered sets of models. You can bind <tt>"change"</tt> events
1050      to be notified when any model in the collection has been modified,
1051      listen for <tt>"add"</tt> and <tt>"remove"</tt> events, <tt>fetch</tt>
1052      the collection from the server, and use a full suite of
1053      <a href="#Collection-Underscore-Methods">Underscore.js methods</a>.
1054    </p>
1055
1056    <p>
1057      Any event that is triggered on a model in a collection will also be
1058      triggered on the collection directly, for convenience.
1059      This allows you to listen for changes to specific attributes in any
1060      model in a collection, for example:
1061      <tt>Documents.bind("change:selected", ...)</tt>
1062    </p>
1063
1064    <p id="Collection-extend">
1065      <b class="header">extend</b><code>Backbone.Collection.extend(properties, [classProperties])</code>
1066      <br />
1067      To create a <b>Collection</b> class of your own, extend <b>Backbone.Collection</b>,
1068      providing instance <b>properties</b>, as well as optional <b>classProperties</b> to be attached
1069      directly to the collection's constructor function.
1070    </p>
1071
1072    <p id="Collection-model">
1073      <b class="header">model</b><code>collection.model</code>
1074      <br />
1075      Override this property to specify the model class that the collection
1076      contains. If defined, you can pass raw attributes objects (and arrays) to
1077      <a href="#Collection-add">add</a>, <a href="#Collection-create">create</a>,
1078      and <a href="#Collection-reset">reset</a>, and the attributes will be
1079      converted into a model of the proper type.
1080    </p>
1081
1082<pre>
1083var Library = Backbone.Collection.extend({
1084  model: Book
1085});
1086</pre>
1087
1088    <p id="Collection-constructor">
1089      <b class="header">constructor / initialize</b><code>new Collection([models], [options])</code>
1090      <br />
1091      When creating a Collection, you may choose to pass in the initial array of <b>models</b>.
1092      The collection's <a href="#Collection-comparator">comparator</a> function
1093      may be included as an option. If you define an <b>initialize</b> function, it will be
1094      invoked when the collection is created.
1095    </p>
1096
1097<pre>
1098var tabs = new TabSet([tab1, tab2, tab3]);
1099</pre>
1100
1101    <p id="Collection-models">
1102      <b class="header">models</b><code>collection.models</code>
1103      <br />
1104      Raw access to the JavaScript array of models inside of the collection. Usually you'll
1105      want to use <tt>get</tt>, <tt>at</tt>, or the <b>Underscore methods</b>
1106      to access model objects, but occasionally a direct reference to the array
1107      is desired.
1108    </p>
1109
1110    <p id="Collection-toJSON">
1111      <b class="header">toJSON</b><code>collection.toJSON()</code>
1112      <br />
1113      Return an array containing the attributes hash of each model in the
1114      collection. This can be used to serialize and persist the
1115      collection as a whole. The name of this method is a bit confusing, because
1116      it conforms to
1117      <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript's JSON API</a>.
1118    </p>
1119
1120<pre class="runnable">
1121var collection = new Backbone.Collection([
1122  {name: "Tim", age: 5},
1123  {name: "Ida", age: 26},
1124  {name: "Rob", age: 55}
1125]);
1126
1127alert(JSON.stringify(collection));
1128</pre>
1129
1130    <p id="Collection-Underscore-Methods">
1131      <b class="header">Underscore Methods (26)</b>
1132      <br />
1133      Backbone proxies to <b>Underscore.js</b> to provide 26 iteration functions
1134      on <b>Backbone.Collection</b>. They aren't all documented here, but
1135      you can take a look at the Underscore documentation for the full details&hellip;
1136    </p>
1137
1138    <ul>
1139      <li><a href="http://documentcloud.github.com/underscore/#each">forEach (each)</a></li>
1140      <li><a href="http://documentcloud.github.com/underscore/#map">map</a></li>
1141      <li><a href="http://documentcloud.github.com/underscore/#reduce">reduce (foldl, inject)</a></li>
1142      <li><a href="http://documentcloud.github.com/underscore/#reduceRight">reduceRight (foldr)</a></li>
1143      <li><a href="http://documentcloud.github.com/underscore/#detect">find (detect)</a></li>
1144      <li><a href="http://documentcloud.github.com/underscore/#select">filter (select)</a></li>
1145      <li><a href="http://documentcloud.github.com/underscore/#reject">reject</a></li>
1146      <li><a href="http://documentcloud.github.com/underscore/#all">every (all)</a></li>
1147      <li><a href="http://documentcloud.github.com/underscore/#any">some (any)</a></li>
1148      <li><a href="http://documentcloud.github.com/underscore/#include">include</a></li>
1149      <li><a href="http://documentcloud.github.com/underscore/#invoke">invoke</a></li>
1150      <li><a href="http://documentcloud.github.com/underscore/#max">max</a></li>
1151      <li><a href="http://documentcloud.github.com/underscore/#min">min</a></li>
1152      <li><a href="http://documentcloud.github.com/underscore/#sortBy">sortBy</a></li>
1153      <li><a href="http://documentcloud.github.com/underscore/#groupBy">groupBy</a></li>
1154      <li><a href="http://documentcloud.github.com/underscore/#sortedIndex">sortedIndex</a></li>
1155      <li><a href="http://documentcloud.github.com/underscore/#toArray">toArray</a></li>
1156      <li><a href="http://documentcloud.github.com/underscore/#size">size</a></li>
1157      <li><a href="http://documentcloud.github.com/underscore/#first">first</a></li>
1158      <li><a href="http://documentcloud.github.com/underscore/#rest">rest</a></li>
1159      <li><a href="http://documentcloud.github.com/underscore/#last">last</a></li>
1160      <li><a href="http://documentcloud.github.com/underscore/#without">without</a></li>
1161      <li><a href="http://documentcloud.github.com/underscore/#indexOf">indexOf</a></li>
1162      <li><a href="http://documentcloud.github.com/underscore/#lastIndexOf">lastIndexOf</a></li>
1163      <li><a href="http://documentcloud.github.com/underscore/#isEmpty">isEmpty</a></li>
1164      <li><a href="http://documentcloud.github.com/underscore/#chain">chain</a></li>
1165    </ul>
1166
1167<pre>
1168Books.each(function(book) {
1169  book.publish();
1170});
1171
1172var titles = Books.map(function(book) {
1173  return book.get("title");
1174});
1175
1176var publishedBooks = Books.filter(function(book) {
1177  return book.get("published") === true;
1178});
1179
1180var alphabetical = Books.sortBy(function(book) {
1181  return book.author.get("name").toLowerCase();
1182});
1183</pre>
1184
1185    <p id="Collection-add">
1186      <b class="header">add</b><code>collection.add(models, [options])</code>
1187      <br />
1188      Add a model (or an array of models) to the collection. Fires an <tt>"add"</tt>
1189      event, which you can pass <tt>{silent: true}</tt> to suppress. If a
1190      <a href="#Collection-model">model</a> property is defined, you may also pass
1191      raw attributes objects, and have them be vivified as instances of the model.
1192      Pass <tt>{at: index}</tt> to splice the model into the collection at the
1193      specified <tt>index</tt>.
1194    </p>
1195
1196<pre class="runnable">
1197var ships = new Backbone.Collection;
1198
1199ships.bind("add", function(ship) {
1200  alert("Ahoy " + ship.get("name") + "!");
1201});
1202
1203ships.add([
1204  {name: "Flying Dutchman"},
1205  {name: "Black Pearl"}
1206]);
1207</pre>
1208
1209    <p id="Collection-remove">
1210      <b class="header">remove</b><code>collection.remove(models, [options])</code>
1211      <br />
1212      Remove a model (or an array of models) from the collection. Fires a
1213      <tt>"remove"</tt> event, which you can use <tt>silent</tt>
1214      to suppress.
1215    </p>
1216
1217    <p id="Collection-get">
1218      <b class="header">get</b><code>collection.get(id)</code>
1219      <br />
1220      Get a model from a collection, specified by <b>id</b>.
1221    </p>
1222
1223<pre>
1224var book = Library.get(110);
1225</pre>
1226
1227    <p id="Collection-getByCid">
1228      <b class="header">getByCid</b><code>collection.getByCid(cid)</code>
1229      <br />
1230      Get a model from a collection, specified by client id. The client id
1231      is the <tt>.cid</tt> property of the model, automatically assigned whenever
1232      a model is created. Useful for models which have not yet been saved to
1233      the server, and do not yet have true ids.
1234    </p>
1235
1236    <p id="Collection-at">
1237      <b class="header">at</b><code>collection.at(index)</code>
1238      <br />
1239      Get a model from a collection, specified by index. Useful if your collection
1240      is sorted, and if your collection isn't sorted, <b>at</b> will still
1241      retrieve models in insertion order.
1242    </p>
1243
1244    <p id="Collection-length">
1245      <b class="header">length</b><code>collection.length</code>
1246      <br />
1247      Like an array, a Collection maintains a <tt>length</tt> property, counting
1248      the number of models it contains.
1249    </p>
1250
1251    <p id="Collection-comparator">
1252      <b class="header">comparator</b><code>collection.comparator</code>
1253      <br />
1254      By default there is no <b>comparator</b> function on a collection.
1255      If you define a comparator, it will be used to maintain
1256      the collection in sorted order. This means that as models are added,
1257      they are inserted at the correct index in <tt>collection.models</tt>.
1258      Comparator functions take a model and return a numeric or string value
1259      by which the model should be ordered relative to others.
1260    </p>
1261
1262    <p>
1263      Note how even though all of the chapters in this example are added backwards,
1264      they come out in the proper order:
1265    </p>
1266
1267<pre class="runnable">
1268var Chapter  = Backbone.Model;
1269var chapters = new Backbone.Collection;
1270
1271chapters.comparator = function(chapter) {
1272  return chapter.get("page");
1273};
1274
1275chapters.add(new Chapter({page: 9, title: "The End"}));
1276chapters.add(new Chapter({page: 5, title: "The Middle"}));
1277chapters.add(new Chapter({page: 1, title: "The Beginning"}));
1278
1279alert(chapters.pluck('title'));
1280</pre>
1281
1282    <p class="warning">
1283      Brief aside: This comparator function is different than JavaScript's regular
1284      "sort", which must return <tt>0</tt>, <tt>1</tt>, or <tt>-1</tt>,
1285      and is more similar to a <tt>sortBy</tt> &mdash; a much nicer API.
1286    </p>
1287
1288    <p class="warning">
1289      Collections with comparator functions will not automatically re-sort if you
1290      later change model attributes, so you may wish to call <tt>sort</tt> after
1291      changing model attributes that would affect the order.
1292    </p>
1293
1294    <p id="Collection-sort">
1295      <b class="header">sort</b><code>collection.sort([options])</code>
1296      <br />
1297      Force a collection to re-sort itself. You don't need to call this under
1298      normal circumstances, as a collection with a <a href="#Collection-comparator">comparator</a> function
1299      will maintain itself in proper sort order at all times. Calling <b>sort</b>
1300      triggers the collection's <tt>"reset"</tt> event, unless silenced by passing
1301      <tt>{silent: true}</tt>
1302    </p>
1303
1304    <p id="Collection-pluck">
1305      <b class="header">pluck</b><code>collection.pluck(attribute)</code>
1306      <br />
1307      Pluck an attribute from each model in the collection. Equivalent to calling
1308      <tt>map</tt>, and returning a single attribute from the iterator.
1309    </p>
1310
1311<pre class="runnable">
1312var stooges = new Backbone.Collection([
1313  new Backbone.Model({name: "Curly"}),
1314  new Backbone.Model({name: "Larry"}),
1315  new Backbone.Model({name: "Moe"})
1316]);
1317
1318var names = stooges.pluck("name");
1319
1320alert(JSON.stringify(names));
1321</pre>
1322
1323    <p id="Collection-url">
1324      <b class="header">url</b><code>collection.url or collection.url()</code>
1325      <br />
1326      Set the <b>url</b> property (or function) on a collection to reference
1327      its location on the server. Models within the collection will use <b>url</b>
1328      to construct URLs of their own.
1329    </p>
1330
1331<pre>
1332var Notes = Backbone.Collection.extend({
1333  url: '/notes'
1334});
1335
1336// Or, something more sophisticated:
1337
1338var Notes = Backbone.Collection.extend({
1339  url: function() {
1340    return this.document.url() + '/notes';
1341  }
1342});
1343</pre>
1344
1345    <p id="Collection-parse">
1346      <b class="header">parse</b><code>collection.parse(response)</code>
1347      <br />
1348      <b>parse</b> is called by Backbone whenever a collection's models are
1349      returned by the server, in <a href="#Collection-fetch">fetch</a>.
1350      The function is passed the raw <tt>response</tt> object, and should return
1351      the array of model attributes to be <a href="#Collection-add">added</a>
1352      to the collection. The default implementation is a no-op, simply passing
1353      through the JSON response. Override this if you need to work with a
1354      preexisting API, or better namespace your responses.
1355    </p>
1356
1357<pre>
1358var Tweets = Backbone.Collection.extend({
1359  // The Twitter Search API returns tweets under "results".
1360  parse: function(response) {
1361    return response.results;
1362  }
1363});
1364</pre>
1365
1366    <p id="Collection-fetch">
1367      <b class="header">fetch</b><code>collection.fetch([options])</code>
1368      <br />
1369      Fetch the default set of models for this collection from the server,
1370      resetting the collection when they arrive. The <b>options</b> hash takes
1371      <tt>success</tt> and <tt>error</tt>
1372      callbacks which will be passed <tt>(collection, response)</tt> as arguments.
1373      When the model data returns from the server, the collection will
1374      <a href="#Collection-reset">reset</a>.
1375      Delegates to <a href="#Sync">Backbone.sync</a>
1376      under the covers, for custom persistence strategies.
1377      The server handler for <b>fetch</b> requests should return a JSON array of
1378      models.
1379    </p>
1380
1381<pre class="runnable">
1382Backbone.sync = function(method, model) {
1383  alert(method + ": " + model.url);
1384};
1385
1386var Accounts = new Backbone.Collection;
1387Accounts.url = '/accounts';
1388
1389Accounts.fetch();
1390</pre>
1391
1392    <p>
1393      If you'd like to add the incoming models to the current collection, instead
1394      of replacing the collection's contents, pass <tt>{add: true}</tt> as an
1395      option to <b>fetch</b>.
1396    </p>
1397
1398    <p>
1399      <b>jQuery.ajax</b> options can also be passed directly as <b>fetch</b> options,
1400      so to fetch a specific page of a paginated collection:
1401      <tt>Documents.fetch({data: {page: 3}})</tt>
1402    </p>
1403
1404    <p>
1405      Note that <b>fetch</b> should not be used to populate collections on
1406      page load &mdash; all models needed at load time should already be
1407      <a href="#FAQ-bootstrap">bootstrapped</a> in to place. <b>fetch</b> is
1408      intended for lazily-loading models for interfaces that are not needed
1409      immediately: for example, documents with collections of notes that may be
1410      toggled open and closed.
1411    </p>
1412
1413    <p id="Collection-reset">
1414      <b class="header">reset</b><code>collection.reset(models, [options])</code>
1415      <br />
1416      Adding and removing models one at a time is all well and good, but sometimes
1417      you have so many models to change that you'd rather just update the collection
1418      in bulk. Use <b>reset</b> to replace a collection with a new list
1419      of models (or attribute hashes), triggering a single <tt>"reset"</tt> event
1420      at the end. Pass <tt>{silent: true}</tt> to suppress the <tt>"reset"</tt> event.
1421      Using reset with no arguments is useful as a way to empty the collection.
1422    </p>
1423
1424    <p>
1425      Here's an example using <b>reset</b> to bootstrap a collection during initial page load,
1426      in a Rails application.
1427    </p>
1428
1429<pre>
1430&lt;script&gt;
1431  Accounts.reset(&lt;%= @accounts.to_json %&gt;);
1432&lt;/script&gt;
1433</pre>
1434
1435    <p>
1436      Calling <tt>collection.reset()</tt> without passing any models as arguments
1437      will empty the entire collection.
1438    </p>
1439
1440    <p id="Collection-create">
1441      <b class="header">create</b><code>collection.create(attributes, [options])</code>
1442      <br />
1443      Convenience to create a new instance of a model within a collection.
1444      Equivalent to instantiating a model with a hash of attributes,
1445      saving the model to the server, and adding the model to the set after being
1446      successfully created. Returns
1447      the model, or <tt>false</tt> if a validation error prevented the
1448      model from being created. In order for this to work, you should set the
1449      <a href="#Collection-model">model</a> property of the collection.
1450      The <b>create</b> method can accept either an attributes hash or an
1451      existing, unsaved model object.
1452    </p>
1453
1454<pre>
1455var Library = Backbone.Collection.extend({
1456  model: Book
1457});
1458
1459var NYPL = new Library;
1460
1461var othello = NYPL.create({
1462  title: "Othello",
1463  author: "William Shakespeare"
1464});
1465</pre>
1466
1467    <h2 id="Router">Backbone.Router</h2>
1468
1469    <p>
1470      Web applications often provide linkable, bookmarkable, shareable URLs for
1471      important locations in the app. Until recently, hash fragments
1472      (<tt>#page</tt>) were used to provide these permalinks, but with the
1473      arrival of the History API, it's now possible to use standard URLs (<tt>/page</tt>).
1474      <b>Backbone.Router</b> provides methods for routing client-side pages, and
1475      connecting them to actions and events. For browsers which don't yet support
1476      the History API, the Router handles graceful fallback and transparent
1477      translation to the fragment version of the URL.
1478    </p>
1479
1480    <p>
1481      During page load, after your application has finished creating all of its routers,
1482      be sure to call <tt>Backbone.history.start()</tt>, or
1483      <tt>Backbone.history.start({pushState: true})</tt> to route the initial URL.
1484    </p>
1485
1486    <p id="Router-extend">
1487      <b class="header">extend</b><code>Backbone.Router.extend(properties, [classProperties])</code>
1488      <br />
1489      Get started by creating a custom router class. Define actions that are
1490      triggered when certain URL fragments are
1491      matched, and provide a <a href="#Router-routes">routes</a> hash
1492      that pairs routes to actions. Note that you'll want to avoid using a
1493      leading slash in your route definitions:
1494    </p>
1495
1496<pre>
1497var Workspace = Backbone.Router.extend({
1498
1499  routes: {
1500    "help":                 "help",    // #help
1501    "search/:query":        "search",  // #search/kiwis
1502    "search/:query/p:page": "search"   // #search/kiwis/p7
1503  },
1504
1505  help: function() {
1506    ...
1507  },
1508
1509  search: function(query, page) {
1510    ...
1511  }
1512
1513});
1514</pre>
1515
1516    <p id="Router-routes">
1517      <b class="header">routes</b><code>router.routes</code>
1518      <br />
1519      The routes hash maps URLs with parameters to functions on your router,
1520      similar to the <a href="#View">View</a>'s <a href="#View-delegateEvents">events hash</a>.
1521      Routes can contain parameter parts, <tt>:param</tt>, which match a single URL
1522      component between slashes; and splat parts <tt>*splat</tt>, which can match
1523      any number of URL components.
1524    </p>
1525
1526    <p>
1527      For example, a route of <tt>"search/:query/p:page"</tt> will match
1528      a fragment of <tt>#search/obama/p2</tt>, passing <tt>"obama"</tt>
1529      and <tt>"2"</tt> to the action. A route of <tt>"file/*path"</tt> will
1530      match <tt>#file/nested/folder/file.txt</tt>,
1531      passing <tt>"nested/folder/file.txt"</tt> to the action.
1532    </p>
1533
1534    <p>
1535      When the visitor presses the back button, or enters a URL, and a particular
1536      route is matched, the name of the action will be fired as an
1537      <a href="#Events">event</a>, so that other objects can listen to the router,
1538      and be notified. In the following example, visiting <tt>#help/uploading</tt>
1539      will fire a <tt>route:help</tt> event from the router.
1540    </p>
1541
1542<pre>
1543routes: {
1544  "help/:page":         "help",
1545  "download/*path":     "download",
1546  "folder/:name":       "openFolder",
1547  "folder/:name-:mode": "openFolder"
1548}
1549</pre>
1550
1551<pre>
1552router.bind("route:help", function(page) {
1553  ...
1554});
1555</pre>
1556
1557    <p id="Router-constructor">
1558      <b class="header">constructor / initialize</b><code>new Router([options])</code>
1559      <br />
1560      When creating a new router, you may pass its
1561      <a href="#Router-routes">routes</a> hash directly as an option, if you
1562      choose. All <tt>options</tt> will also be passed to your <tt>initialize</tt>
1563      function, if defined.
1564    </p>
1565
1566    <p id="Router-route">
1567      <b class="header">route</b><code>router.route(route, name, callback)</code>
1568      <br />
1569      Manually create a route for the router, The <tt>route</tt> argument may
1570      be a <a href="#Router-routes">routing string</a> or regular expression.
1571      Each matching capture from the route or regular expression will be passed as
1572      an argument to the callback. The <tt>name</tt> argument will be triggered as
1573      a <tt>"route:name"</tt> event whenever the route is matched.
1574    </p>
1575
1576<pre>
1577initialize: function(options) {
1578
1579  // Matches #page/10, passing "10"
1580  this.route("page/:number", "page", function(number){ ... });
1581
1582  // Matches /117-a/b/c/open, passing "117-a/b/c"
1583  this.route(/^(.*?)\/open$/, "open", function(id){ ... });
1584
1585}
1586</pre>
1587
1588    <p id="Router-navigate">
1589      <b class="header">navigate</b><code>router.navigate(fragment, [options])</code>
1590      <br />
1591      Whenever you reach a point in your application that you'd like to save
1592      as a URL, call <b>navigate</b> in order to update the URL.
1593      If you wish to also call the route function, set the <b>trigger</b>
1594      option to <tt>true</tt>.
1595      To update the URL without creating an entry in the browser's history,
1596      set the <b>replace</b> option to <tt>true</tt>.
1597    </p>
1598
1599<pre>
1600openPage: function(pageNumber) {
1601  this.document.pages.at(pageNumber).open();
1602  this.navigate("page/" + pageNumber);
1603}
1604
1605# Or ...
1606
1607app.navigate("help/troubleshooting", {trigger: true});
1608
1609# Or ...
1610
1611app.navigate("help/troubleshooting", {trigger: true, replace: true});
1612</pre>
1613
1614    <h2 id="History">Backbone.history</h2>
1615
1616    <p>
1617      <b>History</b> serves as a global router (per frame) to handle <tt>hashchange</tt>
1618      events or <tt>pushState</tt>, match the appropriate route, and trigger callbacks. You shouldn't
1619      ever have to create one of these yourself &mdash; you should use the reference
1620      to <tt>Backbone.history</tt> that will be created for you automatically if you make use
1621      of <a href="#Router">Routers</a> with <a href="#Router-routes">routes</a>.
1622    </p>
1623
1624    <p>
1625      <b>pushState</b> support exists on a purely opt-in basis in Backbone.
1626      Older browsers that don't support <tt>pushState</tt> will continue to use
1627      hash-based URL fragments, and if a hash URL is visited by a
1628      <tt>pushState</tt>-capable browser, it will be transparently upgraded to
1629      the true URL. Note that using real URLs requires your web server to be
1630      able to correctly render those pages, so back-end changes are required
1631      as well. For example, if you have a route of <tt>/documents/100</tt>,
1632      your web server must be able to serve that page, if the browser
1633      visits that URL directly. For full search-engine crawlability, it's best to
1634      have the server generate the complete HTML for the page ... but if it's a web
1635      application, just rendering the same content you would have for the root URL,
1636      and filling in the rest with Backbone Views and JavaScript works fine.
1637    </p>
1638
1639    <p id="History-start">
1640      <b class="header">start</b><code>Backbone.history.start([options])</code>
1641      <br />
1642      When all of your <a href="#Router">Routers</a> have been created,
1643      and all of the routes are set up properly, call <tt>Backbone.history.start()</tt>
1644      to begin monitoring <tt>hashchange</tt> events, and dispatching routes.
1645    </p>
1646
1647    <p>
1648      To indicate that you'd like to use HTML5 <tt>pushState</tt> support in
1649      your application, use <tt>Backbone.history.start({pushState: true})</tt>.
1650    </p>
1651
1652    <p>
1653      If your application is not being served from the root url <tt>/</tt> of your
1654      domain, be sure to tell History where the root really is, as an option:
1655      <tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt>
1656    </p>
1657
1658    <p>
1659      When called, if a route succeeds with a match for the current URL,
1660      <tt>Backbone.history.start()</tt> returns <tt>true</tt>. If no defined
1661      route matches the current URL, it returns <tt>false</tt>.
1662    </p>
1663
1664    <p>
1665      If the server has already rendered the entire page, and you don't want the
1666      initial route to trigger when starting History, pass <tt>silent: true</tt>.
1667    </p>
1668
1669    <p>
1670      Because hash-based history in Internet Explorer relies on an
1671      <tt>&lt;iframe&gt;</tt>, be sure to only call <tt>start()</tt> after the DOM
1672      is ready.
1673    </p>
1674
1675<pre>
1676$(function(){
1677  new WorkspaceRouter();
1678  new HelpPaneRouter();
1679  Backbone.history.start({pushState: true});
1680});
1681</pre>
1682
1683    <h2 id="Sync">Backbone.sync</h2>
1684
1685    <p>
1686      <b>Backbone.sync</b> is the function that Backbone calls every time it
1687      attempts to read or save a model to the server. By default, it uses
1688      <tt>(jQuery/Zepto).ajax</tt> to make a RESTful JSON request. You can override
1689      it in order to use a different persistence strategy, such as WebSockets,
1690      XML transport, or Local Storage.
1691    </p>
1692
1693    <p>
1694      The method signature of <b>Backbone.sync</b> is <tt>sync(method, model, [options])</tt>
1695    </p>
1696
1697    <ul>
1698      <li><b>method</b> – the CRUD method (<tt>"create"</tt>, <tt>"read"</tt>, <tt>"update"</tt>, or <tt>"delete"</tt>)</li>
1699      <li><b>model</b> – the model to be saved (or collection to be read)</li>
1700      <li><b>options</b> – success and error callbacks, and all other jQuery request options</li>
1701    </ul>
1702
1703    <p>
1704      With the default implementation, when <b>Backbone.sync</b> sends up a request to save
1705      a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body
1706      with content-type <tt>application/json</tt>. When returning a JSON response,
1707      send down the attributes of the  model that have been changed by the server, and need
1708      to be updated on the client. When responding to a <tt>"read"</tt> request from a collection
1709      (<a href="#Collection#fetch">Collection#fetch</a>), send down an array
1710      of model attribute objects.
1711    </p>
1712
1713    <p>
1714      The default <b>sync</b> handler maps CRUD to REST like so:
1715    </p>
1716
1717    <ul>
1718      <li><b>create &rarr; POST &nbsp; </b><tt>/collection</tt></li>
1719      <li><b>read &rarr; GET &nbsp; </b><tt>/collection[/id]</tt></li>
1720      <li><b>update &rarr; PUT &nbsp; </b><tt>/collection/id</tt></li>
1721      <li><b>delete &rarr; DELETE &nbsp; </b><tt>/collection/id</tt></li>
1722    </ul>
1723
1724    <p>
1725      As an example, a Rails handler responding to an <tt>"update"</tt> call from
1726      <tt>Backbone</tt> might look like this: <i>(In real code, never use
1727      </i><tt>update_attributes</tt><i> blindly, and always whitelist the attributes
1728      you allow to be changed.)</i>
1729    </p>
1730
1731<pre>
1732def update
1733  account = Account.find params[:id]
1734  account.update_attributes params
1735  render :json => account
1736end
1737</pre>
1738
1739    <p>
1740      One more tip for Rails integration is to disable the default namespacing for
1741      <tt>to_json</tt> calls on models by setting <tt>ActiveRecord::Base.include_root_in_json = false</tt>
1742    </p>
1743
1744    <p id="Sync-emulateHTTP">
1745      <b class="header">emulateHTTP</b><code>Backbone.emulateHTTP = true</code>
1746      <br />
1747      If you want to work with a legacy web server that doesn't support Backbones's
1748      default REST/HTTP approach, you may choose to turn on <tt>Backbone.emulateHTTP</tt>.
1749      Setting this option will fake <tt>PUT</tt> and <tt>DELETE</tt> requests with
1750      a HTTP <tt>POST</tt>, and pass them under the <tt>_method</tt> parameter. Setting this option
1751      will also set an <tt>X-HTTP-Method-Override</tt> header with the true method.
1752    </p>
1753
1754<pre>
1755Backbone.emulateHTTP = true;
1756
1757model.save();  // POST to "/collection/id", with "_method=PUT" + header.
1758</pre>
1759
1760    <p id="Sync-emulateJSON">
1761      <b class="header">emulateJSON</b><code>Backbone.emulateJSON = true</code>
1762      <br />
1763      If you're working with a legacy web server that can't handle requests
1764      encoded as <tt>application/json</tt>, setting <tt>Backbone.emulateJSON = true;</tt>
1765      will cause the JSON to be serialized under a <tt>model</tt> parameter, and
1766      the request to be made with a <tt>application/x-www-form-urlencoded</tt>
1767      mime type, as if from an HTML form.
1768    </p>
1769
1770    <h2 id="View">Backbone.View</h2>
1771
1772    <p>
1773      Backbone views are almost more convention than they are code &mdash; they
1774      don't determine anything about your HTML or CSS for you, and can be used
1775      with any JavaScript templating library.
1776      The general idea is to organize your interface into logical views,
1777      backed by models, each of which can be updated independently when the
1778      model changes, without having to redraw the page. Instead of digging into
1779      a JSON object, looking up an element in the DOM, and updating the HTML by hand,
1780      you can bind your view's <tt>render</tt> function to the model's <tt>"change"</tt>
1781      event &mdash; and now everywhere that
1782      model data is displayed in the UI, it is always immediately up to date.
1783    </p>
1784
1785    <p id="View-extend">
1786      <b class="header">extend</b><code>Backbone.View.extend(properties, [classProperties])</code>
1787      <br />
1788      Get started with views by creating a custom view class. You'll want to
1789      override the <a href="#View-render">render</a> function, specify your
1790      declarative <a href="#View-delegateEvents">events</a>, and perhaps the
1791      <tt>tagName</tt>, <tt>className</tt>, or <tt>id</tt> of the View's root
1792      element.
1793    </p>
1794
1795<pre>
1796var DocumentRow = Backbone.View.extend({
1797
1798  tagName: "li",
1799
1800  className: "document-row",
1801
1802  events: {
1803    "click .icon":          "open",
1804    "click .button.edit":   "openEditDialog",
1805    "click .button.delete": "destroy"
1806  },
1807
1808  render: function() {
1809    ...
1810  }
1811
1812});
1813</pre>
1814
1815    <p id="View-constructor">
1816      <b class="header">constructor / initialize</b><code>new View([options])</code>
1817      <br />
1818      When creating a new View, the options you pass are attached to the view
1819      as <tt>this.options</tt>, for future reference. There are several special
1820      options that, if passed, will be attached directly to the view:
1821      <tt>model</tt>, <tt>collection</tt>,
1822      <tt>el</tt>, <tt>id</tt>, <tt>className</tt>, <tt>tagName</tt> and <tt>attributes</tt>.
1823      If the view defines an <b>initialize</b> function, it will be called when
1824      the view is first created. If you'd like to create a view that references
1825      an element <i>already</i> in the DOM, pass in the element as an option:
1826      <tt>new View({el: existingElement})</tt>
1827    </p>
1828
1829<pre>
1830var doc = Documents.first();
1831
1832new DocumentRow({
1833  model: doc,
1834  id: "document-row-" + doc.id
1835});
1836</pre>
1837
1838    <p id="View-el">
1839      <b class="header">el</b><code>view.el</code>
1840      <br />
1841      All views have a DOM element at all times (the <b>el</b> property),
1842      whether they've already been inserted into the page or not. In this
1843      fashion, views can be rendered at any time, and inserted into the DOM all
1844      at once, in order to get high-performance UI rendering with as few
1845      reflows and repaints as possible. <tt>this.el</tt> is created from the
1846      view's <tt>tagName</tt>, <tt>className</tt>, <tt>id</tt> and <tt>attributes</tt> properties,
1847      if specified. If not, <b>el</b> is an empty <tt>div</tt>.
1848    </p>
1849
1850    <p>
1851      You may assign <b>el</b> directly if the view is being
1852      created for an element that already exists in the DOM. Use either a
1853      reference to a real DOM element, or a css selector string.
1854    </p>
1855
1856<pre class="runnable">
1857var ItemView = Backbone.View.extend({
1858  tagName: 'li'
1859});
1860
1861var BodyView = Backbone.View.extend({
1862  el: 'body'
1863});
1864
1865var item = new ItemView();
1866var body = new BodyView();
1867
1868alert(item.el + ' ' + body.el);
1869</pre>
1870
1871    <p id="View-dollar">
1872      <b class="header">$ (jQuery or Zepto)</b><code>view.$(selector)</code>
1873      <br />
1874      If jQuery or Zepto is included on the page, each view has a
1875      <b>$</b> function that runs queries scoped within the view's element. If you use this
1876      scoped jQuery function, you don't have to use model ids as part of your query
1877      to pull out specific elements in a list, and can rely much more on HTML class
1878      attributes. It's equivalent to running: <tt>$(selector, this.el)</tt>
1879    </p>
1880
1881<pre>
1882ui.Chapter = Backbone.View.extend({
1883  serialize : function() {
1884    return {
1885      title: this.$(".title").text(),
1886      start: this.$(".start-page").text(),
1887      end:   this.$(".end-page").text()
1888    };
1889  }
1890});
1891</pre>
1892
1893    <p id="View-render">
1894      <b class="header">render</b><code>view.render()</code>
1895      <br />
1896      The default implementation of <b>render</b> is a no-op. Override this
1897      function with your code that renders the view template from model data,
1898      and updates <tt>this.el</tt> with the new HTML. A good
1899      convention is to <tt>return this</tt> at the end of <b>render</b> to
1900      enable chained calls.
1901    </p>
1902
1903<pre>
1904var Bookmark = Backbone.View.extend({
1905  render: function() {
1906    $(this.el).html(this.template(this.model.toJSON()));
1907    return this;
1908  }
1909});
1910</pre>
1911
1912    <p>
1913      Backbone is agnostic with respect to your preferred method of HTML templating.
1914      Your <b>render</b> function could even munge together an HTML string, or use
1915      <tt>document.createElement</tt> to generate a DOM tree. However, we suggest
1916      choosing a nice JavaScript templating library.
1917      <a href="http://github.com/janl/mustache.js">Mustache.js</a>,
1918      <a href="http://github.com/creationix/haml-js">Haml-js</a>, and
1919      <a href="http://github.com/sstephenson/eco">Eco</a> are all fine alternatives.
1920      Because <a href="http://documentcloud.github.com/underscore/">Underscore.js</a> is already on the page,
1921      <a href="http://documentcloud.github.com/underscore/#template">_.template</a>
1922      is available, and is an excellent choice if you've already XSS-sanitized
1923      your interpolated data.
1924    </p>
1925
1926    <p>
1927      Whatever templating strategy you end up with, it's nice if you <i>never</i>
1928      have to put strings of HTML in your JavaScript. At DocumentCloud, we
1929      use <a href="http://documentcloud.github.com/jammit/">Jammit</a> in order
1930      to package up JavaScript templates stored in <tt>/app/views</tt> as part
1931      of our main <tt>core.js</tt> asset package.
1932    </p>
1933
1934    <p id="View-remove">
1935      <b class="header">remove</b><code>view.remove()</code>
1936      <br />
1937      Convenience function for removing the view from the DOM. Equivalent to calling
1938      <tt>$(view.el).remove();</tt>
1939    </p>
1940
1941    <p id="View-make">
1942      <b class="header">make</b><code>view.make(tagName, [attributes], [content])</code>
1943      <br />
1944      Convenience function for creating a DOM element of the given type (<b>tagName</b>),
1945      with optional attributes and HTML content. Used internally to create the
1946      initial <tt>view.el</tt>.
1947    </p>
1948
1949<pre class="runnable">
1950var view = new Backbone.View;
1951
1952var el = view.make("b", {"class": "bold"}, "Bold! ");
1953
1954$("#make-demo").append(el);
1955</pre>
1956
1957<div id="make-demo"></div>
1958
1959    <p id="View-delegateEvents">
1960      <b class="header">delegateEvents</b><code>delegateEvents([events])</code>
1961      <br />
1962      Uses jQuery's <tt>delegate</tt> function to provide declarative callbacks
1963      for DOM events within a view.
1964      If an <b>events</b> hash is not passed directly, uses <tt>this.events</tt>
1965      as the source. Events are written in the format <tt>{"event selector": "callback"}</tt>.
1966      Omitting the <tt>selector</tt> causes the event to be bound to the view's
1967      root element (<tt>this.el</tt>). By default, <tt>delegateEvents</tt> is called
1968      within the View's constructor for you, so if you have a simple <tt>events</tt>
1969      hash, all of your DOM events will always already be connected, and you will
1970      never have to call this function yourself.
1971    </p>
1972
1973    <p>
1974      The <tt>events</tt> property may also be defined as a function that returns
1975      an <b>events</b> hash, to make it easier to programmatically define your
1976      events, as well as inherit them from parent views.
1977    </p>
1978
1979    <p>
1980      Using <b>delegateEvents</b> provides a number of advantages over manually
1981      using jQuery to bind events to child elements during <a href="#View-render">render</a>. All attached
1982      callbacks are bound to the view before being handed off to jQuery, so when
1983      the callbacks are invoked, <tt>this</tt> continues to refer to the view object. When
1984      <b>delegateEvents</b> is run again, perhaps with a different <tt>events</tt>
1985      hash, all callbacks are removed and delegated afresh &mdash; useful for
1986      views which need to behave differently when in different modes.
1987    </p>
1988
1989    <p>
1990      A view that displays a document in a search result might look
1991      something like this:
1992    </p>
1993
1994<pre>
1995var DocumentView = Backbone.View.extend({
1996
1997  events: {
1998    "dblclick"                : "open",
1999    "click .icon.doc"         : "select",
2000    "contextmenu .icon.doc"   : "showMenu",
2001    "click .show_notes"       : "toggleNotes",
2002    "click .title .lock"      : "editAccessLevel",
2003    "mouseover .title .date"  : "showTooltip"
2004  },
2005
2006  render: function() {
2007    $(this.el).html(this.template(this.model.toJSON()));
2008    return this;
2009  },
2010
2011  open: function() {
2012    window.open(this.model.get("viewer_url"));
2013  },
2014
2015  select: function() {
2016    this.model.set({selected: true});
2017  },
2018
2019  ...
2020
2021});
2022</pre>
2023
2024    <h2 id="Utility">Utility Functions</h2>
2025
2026    <p>
2027
2028    </p>
2029
2030    <p id="Utility-noConflict">
2031      <b class="header">noConflict</b><code>var backbone = Backbone.noConflict();</code>
2032      <br />
2033      Returns the <tt>Backbone</tt> object back to its original value. You can
2034      use the return value of <tt>Backbone.noConflict()</tt> to keep a local
2035      reference to Backbone. Useful for embedding Backbone on third-party
2036      websites, where you don't want to clobber the existing Backbone.
2037    </p>
2038
2039<pre>
2040var localBackbone = Backbone.noConflict();
2041var model = localBackbone.Model.extend(...);
2042</pre>
2043
2044    <h2 id="examples">Examples</h2>
2045
2046    <p id="examples-todos">
2047      <a href="http://jgn.me/">Jérôme Gravel-Niquet</a> has contributed a
2048      <a href="examples/todos/index.html">Todo List application</a>
2049      that is bundled in the repository as Backbone example. If you're wondering
2050      where to get started with Backbone in general, take a moment to
2051      <a href="docs/todos.html">read through the annotated source</a>. The app uses a
2052      <a href="docs/backbone-localstorage.html">LocalStorage adapter</a>
2053      to transparently save all of your todos within your browser, instead of
2054      sending them to a server. Jérôme also has a version hosted at
2055      <a href="http://localtodos.com/">localtodos.com</a> that uses a
2056      <a href="http://github.com/jeromegn/backbone-mootools">MooTools-backed version of Backbone</a>
2057      instead of jQuery.
2058    </p>
2059
2060    <div style="text-align: center;">
2061      <a href="examples/todos/index.html">
2062        <img src="docs/images/todos.png" alt="Todos" class="example_image" />
2063      </a>
2064    </div>
2065
2066    <h2 id="examples-documentcloud">DocumentCloud</h2>
2067
2068    <p>
2069      The <a href="http://www.documentcloud.org/public/#search/">DocumentCloud workspace</a>
2070      is built on Backbone.js, with <i>Documents</i>, <i>Projects</i>,
2071      <i>Notes</i>, and <i>Accounts</i> all as Backbone models and collections.
2072      If you're interested in history &mdash; both Underscore.js and Backbone.js
2073      were originally extracted from the DocumentCloud codebase, and packaged
2074      into standalone JS libraries.
2075    </p>
2076
2077    <div style="text-align: center;">
2078      <a href="http://www.documentcloud.org/public/#search/">
2079        <img src="docs/images/dc-workspace.png" alt="DocumentCloud Workspace" class="example_image" />
2080      </a>
2081    </div>
2082
2083    <h2 id="examples-linkedin">LinkedIn Mobile</h2>
2084
2085    <p>
2086      <a href="http://www.linkedin.com/">LinkedIn</a> used Backbone.js to create
2087      its <a href="http://www.linkedin.com/static?key=mobile">next-generation HTML5 mobile web app</a>.
2088      Backbone made it easy to keep the app modular, organized and extensible so
2089      that it was possible to program the complexities of LinkedIn's user experience.
2090      The app also uses <a href="http://zeptojs.com/">Zepto</a>,
2091      <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>,
2092      <a href="http://sass-lang.com/">SASS</a>, <a href="http://cubiq.org/iscroll">iScroll</a>,
2093      HTML5 LocalStorage and Canvas.
2094    </p>
2095
2096    <div style="text-align: center;">
2097      <a href="http://www.linkedin.com/static?key=mobile">
2098        <img src="docs/images/linkedin-mobile.png" alt="LinkedIn Mobile" class="example_image" />
2099      </a>
2100    </div>
2101
2102    <h2 id="examples-flow">Flow</h2>
2103
2104    <p>
2105      <a href="http://www.metalabdesign.com/">MetaLab</a> used Backbone.js to create
2106      <a href="http://www.getflow.com/">Flow</a>, a task management app for teams. The
2107      workspace relies on Backbone.js to construct task views, activities, accounts,
2108      folders, projects, and tags. You can see the internals under <tt>window.Flow</tt>.
2109    </p>
2110
2111    <div style="text-align: center;">
2112      <a href="http://www.getflow.com/">
2113        <img src="docs/images/flow.png" alt="Flow" class="example_image" />
2114      </a>
2115    </div>
2116
2117    <h2 id="examples-audiovroom">AudioVroom</h2>
2118
2119    <p>
2120      <a href="http://www.audiovroom.com/">AudioVroom</a> is a free music
2121      streaming app that allows you to listen to your Facebook friends like
2122      radio stations. It relies heavily on Backbone (views and song management)
2123      and also features a responsive grid-based design (using CSS3 media-queries)
2124      to deliver a unified user experience on desktops, mobiles, and tablets alike.
2125      Being a pure Backbone app, AudioVroom is only 60kb compressed, and can be
2126      entirely hosted on the CDN.
2127    </p>
2128
2129    <div style="text-align: center;">
2130      <a href="http://www.audiovroom.com/">
2131        <img src="docs/images/audiovroom.png" alt="AudioVroom" class="example_image" />
2132      </a>
2133    </div>
2134
2135    <h2 id="examples-foursquare">Foursquare</h2>
2136
2137    <p>
2138      Foursquare is a fun little startup that helps you meet up with friends,
2139      discover new places, and save money. Backbone Models are heavily used in
2140      the core JavaScript API layer and Views power many popular features like
2141      the <a href="https://foursquare.com">homepage map</a> and
2142      <a href="https://foursquare.com/seriouseats/list/the-best-doughnuts-in-ny">lists</a>.
2143    </p>
2144
2145    <div style="text-align: center;">
2146      <a href="http://foursquare.com">
2147        <img src="docs/images/foursquare.png" alt="Foursquare" class="example_image" />
2148      </a>
2149    </div>
2150
2151    <h2 id="examples-do">Do</h2>
2152
2153    <p>
2154      <a href="http://do.com">Do</a> is a social productivity app that makes it
2155      easy to work on tasks, track projects, and take notes with your team.
2156      The <a href="http://do.com">Do.com</a> web application was built from the
2157      ground up to work seamlessly on your smartphone, tablet and computer. The
2158      team used Backbone, CoffeeScript and Handlebars to build a full-featured
2159      app in record time and rolled their own extensions for complex navigation
2160      and model sync support.
2161    </p>
2162
2163    <div style="text-align: center;">
2164      <a href="http://do.com">
2165        <img src="docs/images/do.png" alt="Do" class="example_image" />
2166      </a>
2167    </div>
2168
2169    <h2 id="examples-posterous">Posterous</h2>
2170
2171    <p>
2172      <a href="https://posterous.com/">Posterous Spaces</a> is
2173      <a href="http://technology.posterous.com/posterous-spaces-is-built-on-backbonejs">built on Backbone</a>.
2174      The models and collections mirror the public
2175      <a href="http://posterous.com/api">Posterous API</a>. Backbone made it easy
2176      for the team to create a JavaScript-heavy application with complex
2177      interactions and state maintenance. Spaces also uses CoffeeScript,
2178      Underscore.js, Haml.js, Sass, Compass, and of course jQuery.
2179    </p>
2180
2181    <div style="text-align: center;">
2182      <a href="https://posterous.com/">
2183        <img src="docs/images/posterous-spaces.png" alt="Posterous Spaces" class="example_image" />
2184      </a>
2185    </div>
2186
2187    <h2 id="examples-groupon">Groupon Now!</h2>
2188
2189    <p>
2190      <a href="http://www.groupon.com/now">Groupon Now!</a> helps you find
2191      local deals that you can buy and use right now. When first developing
2192      the product, the team decided it would be AJAX heavy with smooth transitions
2193      between sections instead of full refreshes, but still needed to be fully
2194      linkable and shareable. Despite never having used Backbone before, the
2195      learning curve was incredibly quick &mdash; a prototype was hacked out in an
2196      afternoon, and the team was able to ship the product in two weeks.
2197      Because the source is minimal and understandable, it was easy to
2198      add several Backbone extensions for Groupon Now!: changing the router
2199      to handle URLs with querystring parameters, and adding a simple
2200      in-memory store for caching repeated requests for the same data.
2201    </p>
2202
2203    <div style="text-align: center;">
2204      <a href="http://www.groupon.com/now">
2205        <img src="docs/images/groupon.png" alt="Groupon Now!" class="example_image" />
2206      </a>
2207    </div>
2208
2209    <h2 id="examples-basecamp">Basecamp Mobile</h2>
2210
2211    <p>
2212      <a href="http://37signals.com/">37Signals</a> used Backbone.js to create
2213      <a href="http://basecamphq.com/mobile">Basecamp Mobile</a>, the mobile version
2214      of their popular project management software. You can access all your Basecamp
2215      projects, post new messages, and comment on milestones (all represented
2216      internally as Backbone.js models).
2217    </p>
2218
2219    <div style="text-align: center;">
2220      <a href="http://basecamphq.com/mobile">
2221        <img src="docs/images/basecamp-mobile.png" alt="Basecamp Mobile" class="example_image" />
2222      </a>
2223    </div>
2224
2225    <h2 id="examples-slavery-footprint">Slavery Footprint</h2>
2226
2227    <p>
2228      <a href="http://slaveryfootprint.org/survey">Slavery Footprint</a>
2229      allows consumers to visualize how their consumption habits are
2230      connected to modern-day slavery and provides them with an opportunity
2231      to have a deeper conversation with the companies that manufacture the
2232      goods they purchased.
2233      Based in Oakland, California, the Slavery Footprint team works to engage
2234      individuals, groups, and businesses to build awareness for and create
2235      deployable action against forced labor, human trafficking, and modern-day
2236      slavery through online tools, as well as off-line community education and
2237      mobilization programs.
2238    </p>
2239
2240    <div style="text-align: center;">
2241      <a href="http://slaveryfootprint.org/survey">
2242        <img src="docs/images/slavery-footprint.png" alt="Slavery Footprint" class="example_image" />
2243      </a>
2244    </div>
2245
2246    <h2 id="examples-diaspora">Diaspora</h2>
2247
2248    <p>
2249      <a href="http://www.joindiaspora.com/">Diaspora</a> is a distributed social
2250      network, formed from a number of independently operated <i>pods</i>.
2251      You own your personal data, and control with whom you share.
2252      All of Diaspora is <a href="https://github.com/diaspora/diaspora">open-source</a>
2253      code, built with Rails and Backbone.js.
2254    </p>
2255
2256    <div style="text-align: center;">
2257      <a href="http://www.joindiaspora.com/">
2258        <img src="docs/images/diaspora.png" alt="Diaspora" class="example_image" />
2259      </a>
2260    </div>
2261
2262    <h2 id="examples-trajectory">Trajectory</h2>
2263
2264    <p>
2265      <a href="https://www.apptrajectory.com/">Trajectory</a> is an agile
2266      software planning tool used to discuss wireframes, record decisions made,
2267      relate user stories and bugs to discussions, and track your progress and
2268      plan the future. With Rails on the backend, Trajectory uses Backbone.js
2269      heavily to provide a fluid story planning interface that even updates in
2270      real-time based on the actions of other users.
2271    </p>
2272
2273    <div style="text-align: center;">
2274      <a href="http://www.apptrajectory.com/">
2275        <img src="docs/images/trajectory.png" alt="Trajectory" class="example_image" />
2276      </a>
2277    </div>
2278
2279    <h2 id="examples-soundcloud">SoundCloud Mobile</h2>
2280
2281    <p>
2282      <a href="http://soundcloud.com">SoundCloud</a> is the leading sound sharing
2283      platform on the internet, and Backbone.js provides the foundation for
2284      <a href="http://m.soundcloud.com">SoundCloud Mobile</a>. The project uses
2285      the public SoundCloud <a href="http://soundcloud.com/developers">API</a>
2286      as a data source (channeled through a nginx proxy),
2287      <a href="http://api.jquery.com/category/plugins/templates/">jQuery templates</a>
2288      for the rendering, <a href="http://docs.jquery.com/Qunit">Qunit
2289      </a> and <a href="http://www.phantomjs.org/">PhantomJS</a> for
2290      the testing suite. The JS code, templates and CSS are built for the
2291      production deployment with various Node.js tools like
2292      <a href="https://github.com/dsimard/ready.js">ready.js</a>,
2293      <a href="https://github.com/mde/jake">Jake</a>,
2294      <a href="https://github.com/tmpvar/jsdom">jsdom</a>.
2295      The <b>Backbone.History</b> was modified to support the HTML5 <tt>history.pushState</tt>.
2296      <b>Backbone.sync</b> was extended with an additional SessionStorage based cache
2297      layer.
2298    </p>
2299
2300    <div style="text-align: center;">
2301      <a href="http://m.soundcloud.com">
2302        <img src="docs/images/soundcloud.png" alt="SoundCloud" class="example_image" />
2303      </a>
2304    </div>
2305
2306    <h2 id="examples-pandora">Pandora</h2>
2307
2308    <p>
2309      When <a href="http://www.pandora.com/newpandora">Pandora</a> redesigned
2310      their site in HTML5, they chose Backbone.js to help
2311      manage the user interface and interactions. For example, there's a model
2312      that represents the "currently playing track", and multiple views that
2313      automatically update when the current track changes. The station list is a
2314      collection, so that when stations are added or changed, the UI stays up to date.
2315    </p>
2316
2317    <div style="text-align: center;">
2318      <a href="http://www.pandora.com/newpandora">
2319        <img src="docs/images/pandora.png" alt="Pandora" class="example_image" />
2320      </a>
2321    </div>
2322
2323    <h2 id="examples-cloudapp">CloudApp</h2>
2324
2325    <p>
2326      <a href="http://getcloudapp.com">CloudApp</a> is simple file and link
2327      sharing for the Mac. Backbone.js powers the web tools
2328      which consume the <a href="http://developer.getcloudapp.com">documented API</a>
2329      to manage Drops. Data is either pulled manually or pushed by
2330      <a href="http://pusher.com">Pusher</a> and fed to
2331      <a href="http://github.com/janl/mustache.js">Mustache</a> templates for
2332      rendering. Check out the <a href="http://cloudapp.github.com/engine">annotated source code</a>
2333      to see the magic.
2334    </p>
2335
2336    <div style="text-align: center;">
2337      <a href="http://getcloudapp.com">
2338        <img src="docs/images/cloudapp.png" alt="CloudApp" class="example_image" />
2339      </a>
2340    </div>
2341
2342    <h2 id="examples-seatgeek">SeatGeek</h2>
2343
2344    <p>
2345      <a href="http://seatgeek.com">SeatGeek</a>'s stadium ticket maps were originally
2346      developed with Prototype.js. Moving to Backbone.js and jQuery helped organize
2347      a lot of the UI code, and the increased structure has made adding features
2348      a lot easier. SeatGeek is also in the process of building a mobile
2349      interface that will be Backbone.js from top to bottom.
2350    </p>
2351
2352    <div style="text-align: center;">
2353      <a href="http://seatgeek.com">
2354        <img src="docs/images/seatgeek.png" alt="SeatGeek" class="example_image" />
2355      </a>
2356    </div>
2357
2358    <h2 id="examples-grove">Grove.io</h2>
2359
2360    <p>
2361      <a href="http://grove.io">Grove.io</a> provides hosted IRC for teams.
2362      Backbone.js powers Grove's web client together with Handlebars.js templating.
2363      Updates to chat stream are pulled in realtime using long-polling.
2364    </p>
2365
2366    <div style="text-align: center;">
2367      <a href="http://grove.io">
2368        <img src="docs/images/grove.png" alt="Grove.io" class="example_image" />
2369      </a>
2370    </div>
2371
2372    <h2 id="examples-kicksend">Kicksend</h2>
2373
2374    <p>
2375      <a href="http://kicksend.com">Kicksend</a> is a real-time file sharing
2376      platform that helps everyday people send and receive files of any size
2377      with their friends and family. Kicksend's web application makes extensive
2378      use of Backbone.js to model files, friends, lists and activity streams.
2379    </p>
2380
2381    <div style="text-align: center;">
2382      <a href="http://kicksend.com">
2383        <img src="docs/images/kicksend.png" alt="Kicksend" class="example_image" />
2384      </a>
2385    </div>
2386
2387    <h2 id="examples-shortmail">Shortmail</h2>
2388
2389    <p>
2390      <a href="http://410labs.com/">410 Labs</a> uses Backbone.js at
2391      <a href="http://shortmail.com/">Shortmail.com</a> to build a
2392      fast and responsive inbox, driven by the <a href="#Router">Router</a>.
2393      Backbone works with a Rails backend to provide inbox rendering, archiving,
2394      replying, composing, and even a changes feed. Using Backbone's event-driven
2395      model and pushing the rendering and interaction logic to the front-end
2396      has not only simplified the view code, it has also drastically reduced the
2397      load on Shortmail's servers.
2398    </p>
2399
2400    <div style="text-align: center;">
2401      <a href="http://shortmail.com">
2402        <img src="docs/images/shortmail.png" alt="Shortmail" class="example_image" />
2403      </a>
2404    </div>
2405
2406    <h2 id="examples-battlefield">Battlefield Play4Free</h2>
2407
2408    <p>
2409      <a href="http://battlefield.play4free.com/">Battlefield Play4Free</a> is
2410      the latest free-to-play first person shooter from the same team that
2411      created Battlefield Heroes. The in-game HTML5 front-end for makes heavy use of
2412      Backbone's views, models and collections to help keep the code modular
2413      and structured.
2414    </p>
2415
2416    <div style="text-align: center;">
2417      <a href="http://battlefield.play4free.com/">
2418        <img src="docs/images/battlefield.png" alt="Battlefield Play4Free" class="example_image" />
2419      </a>
2420    </div>
2421
2422    <h2 id="examples-salon">Salon.io</h2>
2423
2424    <p>
2425      <a href="http://salon.io">Salon.io</a> provides a space where photographers,
2426      artists and designers freely arrange their visual art on virtual walls.
2427      <a href="http://salon.io">Salon.io</a> runs on Rails, but does not use
2428      much of the traditional stack, as the entire frontend is designed as a
2429      single page web app, using Backbone.js and
2430      <a href="http://coffeescript.org">CoffeeScript</a>.
2431    </p>
2432
2433    <div style="text-align: center;">
2434      <a href="http://salon.io">
2435        <img src="docs/images/salon.png" alt="Salon.io" class="example_image" />
2436      </a>
2437    </div>
2438
2439    <h2 id="examples-quoteroller">Quote Roller</h2>
2440
2441    <p>
2442      <a href="http://www.codingstaff.com">Coding Staff</a> used Backbone.js to
2443      create <a href="http://www.quoteroller.com">Quote Roller</a>, an application
2444      that helps to create, send, organize and track business proposals with ease.
2445      Backbone.js has been used to implement interactive parts of the
2446      application like template builder, pricing table, file attachments manager.
2447    </p>
2448
2449    <div style="text-align: center;">
2450      <a href="http://www.quoteroller.com">
2451        <img src="docs/images/quoteroller.png" alt="Quote Roller" class="example_image" />
2452      </a>
2453    </div>
2454
2455    <h2 id="examples-tilemill">TileMill</h2>
2456
2457    <p>
2458      Our fellow
2459      <a href="http://www.newschallenge.org/">Knight Foundation News Challenge</a>
2460      winners, <a href="http://mapbox.com/">MapBox</a>, created an open-source
2461      map design studio with Backbone.js:
2462      <a href="http://mapbox.github.com/tilemill/">TileMill</a>.
2463      TileMill lets you manage map layers based on shapefiles and rasters, and
2464      edit their appearance directly in the browser with the
2465      <a href="https://github.com/mapbox/carto">Carto styling language</a>.
2466      Note that the gorgeous <a href="http://mapbox.com/">MapBox</a> homepage
2467      is also a Backbone.js app.
2468    </p>
2469
2470    <div style="text-align: center;">
2471      <a href="http://mapbox.github.com/tilemill/">
2472        <img src="docs/images/tilemill.png" alt="TileMill" class="example_image" />
2473      </a>
2474    </div>
2475
2476    <h2 id="examples-rround">rround.me</h2>
2477
2478    <p>
2479      <a href="http://rround.me">rround.me</a> uses the HTML
2480      <a href="http://diveintohtml5.org/geolocation.html">Geolocation API</a>
2481      to discover tweets, YouTube videos, Instagram photos, Foursquare spots, and other happenings
2482      posted by people close to you. The site is a single-page app built on Backbone.js,
2483      with people, locations and events all represented by Backbone Models.
2484      Backbone Views listen for changes in the underlying data as you adjust
2485      the range (distance) of your search.
2486    </p>
2487
2488    <div style="text-align: center;">
2489      <a href="http://rround.me/">
2490        <img src="docs/images/rround.png" alt="rround.me" class="example_image" />
2491      </a>
2492    </div>
2493
2494    <h2 id="examples-blossom">Blossom</h2>
2495
2496    <p>
2497      <a href="http://blossom.io">Blossom</a> is a lightweight project management
2498      tool for lean teams. Backbone.js is heavily used in combination with
2499      <a href="http://coffeescript.org">CoffeeScript</a> to provide a smooth
2500      interaction experience. The RESTful backend is built
2501      with <a href="http://flask.pocoo.org/">Flask</a> on Google App Engine.
2502    </p>
2503
2504    <div style="text-align: center;">
2505      <a href="http://blossom.io">
2506        <img src="docs/images/blossom.png" alt="Blossom" class="example_image" />
2507      </a>
2508    </div>
2509
2510    <h2 id="examples-instagreat">Insta-great!</h2>
2511
2512    <p>
2513      <a href="http://twitter.com/elliottkember">Elliott Kember</a> and
2514      <a href="http://twitter.com/dizzyup">Hector Simpson</a> built
2515      <a href="http://instagre.at">Insta-great!</a>
2516      - a fun way to explore popular photos and interact with
2517      <a href="http://instagram.com/">Instagram</a> on the web.
2518      Elliott says, "Backbone.js and Coffeescript were insanely useful for
2519      writing clean, consistent UI code and keeping everything modular and
2520      readable, even through several code refactors. I'm in love."
2521    </p>
2522
2523    <div style="text-align: center;">
2524      <a href="http://instagre.at">
2525        <img src="docs/images/instagreat.png" alt="instagre.at" class="example_image" />
2526      </a>
2527    </div>
2528
2529    <h2 id="examples-decide">Decide</h2>
2530
2531    <p>
2532      <a href="http://decide.com">Decide.com</a> helps people decide when to buy
2533      consumer electronics. It relies heavily on Backbone.js to render and
2534      update its Search Results Page. An "infinite scroll" feature takes
2535      advantage of a SearchResults model containing a collection of
2536      Product models to fetch more results and render them on the fly
2537      with Mustache. A SearchController keeps everything in sync and
2538      maintains page state in the URL. Backbone also powers the user
2539      accounts and settings management.
2540    </p>
2541
2542    <div style="text-align: center;">
2543      <a href="http://decide.com">
2544        <img src="docs/images/decide.png" alt="Decide" class="example_image" />
2545      </a>
2546    </div>
2547
2548    <h2 id="examples-trello">Trello</h2>
2549
2550    <p>
2551      <a href="http://trello.com">Trello</a> is a collaboration tool that
2552      organizes your projects into boards. A Trello board holds many lists of
2553      cards, which can contain checklists, files and conversations, and may be
2554      voted on and organized with labels. Updates on the board happen in
2555      real time. The site was built ground up using Backbone.js for all the
2556      models, views, and routes.
2557    </p>
2558
2559    <div style="text-align: center;">
2560      <a href="http://trello.com">
2561        <img src="docs/images/trello.png" alt="Trello" class="example_image" />
2562      </a>
2563    </div>
2564
2565    <h2 id="examples-bittorrent">BitTorrent</h2>
2566
2567    <p>
2568      <a href="http://www.bittorrent.com">BitTorrent</a> used Backbone to
2569      completely rework an existing Win32 UI. Models normalize access to the
2570      client's data and views rely heavily on the <tt>change</tt> events to keep
2571      the UI state current. Using Backbone and SCSS,
2572      <a href="http://www.bittorrent.com/chrysalis/">our new design</a> and UX
2573      prototypes are considerably easier to iterate, test and work with than
2574      the original Win32 UI.
2575    </p>
2576
2577    <div style="text-align: center;">
2578      <a href="http://www.bittorrent.com/chrysalis/">
2579        <img src="docs/images/bittorrent.jpg" alt="BitTorrent" class="example_image" />
2580      </a>
2581    </div>
2582
2583    <h2 id="examples-picplum">Picplum</h2>
2584
2585    <p>
2586      <a href="https://www.picplum.com/">Picplum</a> makes it easy to
2587      automatically send high quality photo prints to
2588      your close family and friends. The entire app is built in Backbone.js
2589      using Handlebars.js templates.
2590      <a href="http://pusher.com/">Pusher</a> enables real-time
2591      uploading that makes the photo import process fast and reactive.
2592    </p>
2593
2594    <div style="text-align: center;">
2595      <a href="https://www.picplum.com/">
2596        <img src="docs/images/picplum.jpg" alt="Picplum" class="example_image" />
2597      </a>
2598    </div>
2599
2600    <h2 id="examples-ducksboard">Ducksboard</h2>
2601
2602    <p>
2603    <a href="http://ducksboard.com/">Ducksboard</a> is an online dashboard
2604      for your SaaS and business metrics, built with
2605      <a href="http://twistedmatrix.com/">Twisted</a> and
2606      <a href="http://www.djangoproject.com/">Django</a> and using WebSockets.
2607      It can fetch data from popular providers or accept input through
2608      a simple API.
2609      Backbone is used throughout Ducksboard's interface, every widget,
2610      dashboard and SaaS account is a Backbone model with several views
2611      (data display, configuration view). A
2612      <a href="https://public.ducksboard.com/BFVzKVPeOoWRsL0VZ8MZ/">live demo</a>
2613      is available.
2614    </p>
2615
2616    <div style="text-align: center;">
2617      <a href="http://ducksboard.com/">
2618        <img src="docs/images/ducksboard.png" alt="Ducksboard" class="example_image" />
2619      </a>
2620    </div>
2621
2622    <h2 id="examples-quietwrite">QuietWrite</h2>
2623
2624    <p>
2625      <a href="http://www.twitter.com/jamesjyu">James Yu</a> used Backbone.js to
2626      create <a href="http://www.quietwrite.com/">QuietWrite</a>, an app
2627      that gives writers a clean and quiet interface to concentrate on the text itself.
2628      The editor relies on Backbone to persist document data to the server. He
2629      followed up with a Backbone.js + Rails tutorial that describes how to implement
2630      <a href="http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/">CloudEdit, a simple document editing app</a>.
2631    </p>
2632
2633    <div style="text-align: center;">
2634      <a href="http://www.quietwrite.com/">
2635        <img src="docs/images/quietwrite.png" alt="QuietWrite" class="example_image" />
2636      </a>
2637    </div>
2638
2639    <h2 id="examples-tzigla">Tzigla</h2>
2640
2641    <p>
2642      <a href="http://twitter.com/evilchelu">Cristi Balan</a> and
2643      <a href="http://dira.ro">Irina Dumitrascu</a> created
2644      <a href="http://tzigla.com">Tzigla</a>, a collaborative drawing
2645      application where artists make tiles that connect to each other to
2646      create <a href="http://tzigla.com/boards/1">surreal drawings</a>.
2647      Backbone models help organize the code, routers provide
2648      <a href="http://tzigla.com/boards/1#!/tiles/2-2">bookmarkable deep links</a>,
2649      and the views are rendered with
2650      <a href="https://github.com/creationix/haml-js">haml.js</a> and
2651      <a href="http://zeptojs.com/">Zepto</a>.
2652      Tzigla is written in Ruby (Rails) on the backend, and
2653      <a href="http://coffeescript.org">CoffeeScript</a> on the frontend, with
2654      <a href="http://documentcloud.github.com/jammit/">Jammit</a>
2655      prepackaging the static assets.
2656    </p>
2657
2658    <div style="text-align: center;">
2659      <a href="http://www.tzigla.com/">
2660        <img src="docs/images/tzigla.png" alt="Tzigla" class="example_image" />
2661      </a>
2662    </div>
2663
2664    <h2 id="faq">F.A.Q.</h2>
2665
2666    <p id="FAQ-events">
2667      <b class="header">Catalog of Events</b>
2668      <br />
2669      Here's a list of all of the built-in events that Backbone.js can fire.
2670      You're also free to trigger your own events on Models and Views as you
2671      see fit.
2672    </p>
2673
2674    <ul>
2675      <li><b>"add"</b> (model, collection) &mdash; when a model is added to a collection. </li>
2676      <li><b>"remove"</b> (model, collection) &mdash; when a model is removed from a collection. </li>
2677      <li><b>"reset"</b> (collection) &mdash; when the collection's entire contents have been replaced. </li>
2678      <li><b>"change"</b> (model, options) &mdash; when a model's attributes have changed. </li>
2679      <li><b>"change:[attribute]"</b> (model, value, options) &mdash; when a specific attribute has been updated. </li>
2680      <li><b>"destroy"</b> (model, collection) &mdash; when a model is <a href="#Model-destroy">destroyed</a>. </li>
2681      <li><b>"error"</b> (model, collection) &mdash; when a model's validation fails, or a <a href="#Model-save">save</a> call fails on the server. </li>
2682      <li><b>"route:[name]"</b> (router) &mdash; when one of a router's routes has matched. </li>
2683      <li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument. </li>
2684    </ul>
2685
2686    <p id="FAQ-tim-toady">
2687      <b class="header">There's More Than One Way To Do It</b>
2688      <br />
2689      It's common for folks just getting started to treat the examples listed
2690      on this page as some sort of gospel truth. In fact, Backbone.js is intended
2691      to be fairly agnostic about many common patterns in client-side code.
2692      For example...
2693    </p>
2694
2695    <p>
2696      <b>References between Models and Views</b> can be handled several ways.
2697      Some people like to have direct pointers, where views correspond 1:1 with
2698      models (<tt>model.view</tt> and <tt>view.model</tt>). Others prefer to have intermediate
2699      "controller" objects that orchestrate the creation and organization of
2700      views into a hierarchy. Others still prefer the evented approach, and always
2701      fire events instead of calling methods directly. All of these styles work well.
2702    </p>
2703
2704    <p>
2705      <b>Batch operations</b> on Models are common, but often best handled differently
2706      depending on your server-side setup. Some folks don't mind making individual
2707      Ajax requests. Others create explicit resources for RESTful batch operations:
2708      <tt>/notes/batch/destroy?ids=1,2,3,4</tt>. Others tunnel REST over JSON, with the
2709      creation of "changeset" requests:
2710    </p>
2711
2712<pre>
2713  {
2714    "create":  [array of models to create]
2715    "update":  [array of models to update]
2716    "destroy": [array of model ids to destroy]
2717  }
2718</pre>
2719
2720    <p>
2721      <b>Feel free to define your own events.</b> <a href="#Events">Backbone.Events</a>
2722      is designed so that you can mix it in to any JavaScript object or prototype.
2723      Since you can use any string as an event, it's often handy to bind
2724      and trigger your own custom events: <tt>model.bind("selected:true")</tt> or
2725      <tt>model.bind("editing")</tt>
2726    </p>
2727
2728    <p>
2729      <b>Render the UI</b> as you see fit. Backbone is agnostic as to whether you
2730      use <a href="http://documentcloud.github.com/underscore/#template">Underscore templates</a>,
2731      <a href="https://github.com/janl/mustache.js">Mustache.js</a>, direct DOM
2732      manipulation, server-side rendered snippets of HTML, or
2733      <a href="http://jqueryui.com/">jQuery UI</a> in your <tt>render</tt> function.
2734      Sometimes you'll create a view for each model ... sometimes you'll have a
2735      view that renders thousands of models at once, in a tight loop. Both can be
2736      appropriate in the same app, depending on the quantity of data involved,
2737      and the complexity of the UI.
2738    </p>
2739
2740    <p id="FAQ-nested">
2741      <b class="header">Nested Models &amp; Collections</b>
2742      <br />
2743      It's common to nest collections inside of models with Backbone. For example,
2744      consider a <tt>Mailbox</tt> model that contains many <tt>Message</tt> models.
2745      One nice pattern for handling this is have a <tt>this.messages</tt> collection
2746      for each mailbox, enabling the lazy-loading of messages, when the mailbox
2747      is first opened ... perhaps with <tt>MessageList</tt> views listening for
2748      <tt>"add"</tt> and <tt>"remove"</tt> events.
2749    </p>
2750
2751<pre>
2752var Mailbox = Backbone.Model.extend({
2753
2754  initialize: function() {
2755    this.messages = new Messages;
2756    this.messages.url = '/mailbox/' + this.id + '/messages';
2757    this.messages.bind("reset", this.updateCounts);
2758  },
2759
2760  ...
2761
2762});
2763
2764var Inbox = new Mailbox;
2765
2766// And then, when the Inbox is opened:
2767
2768Inbox.messages.fetch();
2769</pre>
2770
2771    <p>
2772      If you're looking for something more opinionated, there are a number of
2773      Backbone plugins that add sophisticated associations among models,
2774      <a href="https://github.com/documentcloud/backbone/wiki/Extensions%2C-Plugins%2C-Resources">available on the wiki</a>.
2775    </p>
2776
2777    <p id="FAQ-bootstrap">
2778      <b class="header">Loading Bootstrapped Models</b>
2779      <br />
2780      When your app first loads, it's common to have a set of initial models that
2781      you know you're going to need, in order to render the page. Instead of
2782      firing an extra AJAX request to <a href="#Collection-fetch">fetch</a> them,
2783      a nicer pattern is to have their data already bootstrapped into the page.
2784      You can then use <a href="#Collection-reset">reset</a> to populate your
2785      collections with the initial data. At DocumentCloud, in the
2786      <a href="http://en.wikipedia.org/wiki/ERuby">ERB</a> template for the
2787      workspace, we do something along these lines:
2788    </p>
2789
2790<pre>
2791&lt;script&gt;
2792  Accounts.reset(&lt;%= @accounts.to_json %&gt;);
2793  Projects.reset(&lt;%= @projects.to_json(:collaborators => true) %&gt;);
2794&lt;/script&gt;
2795</pre>
2796
2797    <p id="FAQ-mvc">
2798      <b class="header">How does Backbone relate to "traditional" MVC?</b>
2799      <br />
2800      Different implementations of the
2801      <a href="http://en.wikipedia.org/wiki/Model–View–Controller">Model-View-Controller</a>
2802      pattern tend to disagree about the definition of a controller. If it helps any, in
2803      Backbone, the <a href="#View">View</a> class can also be thought of as a
2804      kind of controller, dispatching events that originate from the UI, with
2805      the HTML template serving as the true view. We call it a View because it
2806      represents a logical chunk of UI, responsible for the contents of a single
2807      DOM element.
2808    </p>
2809
2810    <p>
2811      Comparing the overall structure of Backbone to a server-side MVC framework
2812      like <b>Rails</b>, the pieces line up like so:
2813    </p>
2814
2815    <ul>
2816      <li>
2817        <b>Backbone.Model</b> &ndash; Like a Rails model minus the class
2818        methods. Wraps a row of data in business logic.
2819      </li>
2820      <li>
2821        <b>Backbone.Collection</b> &ndash; A group of models on the client-side,
2822        with sorting/filtering/aggregation logic.
2823      </li>
2824      <li>
2825        <b>Backbone.Router</b> &ndash; Rails <tt>routes.rb</tt> + Rails controller
2826        actions. Maps URLs to functions.
2827      </li>
2828      <li>
2829        <b>Backbone.View</b> &ndash; A logical, re-usable piece of UI. Often,
2830        but not always, associated with a model.
2831      </li>
2832      <li>
2833        <b>Client-side Templates</b> &ndash; Rails <tt>.html.erb</tt> views,
2834        rendering a chunk of HTML.
2835      </li>
2836    </ul>
2837
2838    <p id="FAQ-this">
2839      <b class="header">Binding "this"</b>
2840      <br />
2841      Perhaps the single most common JavaScript "gotcha" is the fact that when
2842      you pass a function as a callback, its value for <tt>this</tt> is lost. With
2843      Backbone, when dealing with <a href="#Events">events</a> and callbacks,
2844      you'll often find it useful to rely on
2845      <a href="http://documentcloud.github.com/underscore/#bind">_.bind</a> and
2846      <a href="http://documentcloud.github.com/underscore/#bindAll">_.bindAll</a>
2847      from Underscore.js.
2848    </p>
2849
2850    <p>
2851      When binding callbacks to Backbone events, you can choose to pass an optional
2852      third argument to specify the <tt>this</tt> that will be used when the
2853      callback is later invoked:
2854    </p>
2855
2856<pre>
2857var MessageList = Backbone.View.extend({
2858
2859  initialize: function() {
2860    var messages = this.collection;
2861    messages.bind("reset", this.render, this);
2862    messages.bind("add", this.addMessage, this);
2863    messages.bind("remove", this.removeMessage, this);
2864  }
2865
2866});
2867
2868// Later, in the app...
2869
2870Inbox.messages.add(newMessage);
2871</pre>
2872
2873    <h2 id="changelog">Change Log</h2>
2874
2875    <p>
2876      <b class="header">0.5.3</b> &mdash; <small><i>August 9, 2011</i></small><br />
2877      A View's <tt>events</tt> property may now be defined as a function, as well
2878      as an object literal, making it easier to programmatically define and inherit
2879      events. <tt>groupBy</tt> is now proxied from Underscore as a method on Collections.
2880      If the server has already rendered everything on page load, pass
2881      <tt>Backbone.history.start({silent: true})</tt> to prevent the initial route
2882      from triggering. Bugfix for pushState with encoded URLs.
2883    </p>
2884
2885    <p>
2886      <b class="header">0.5.2</b> &mdash; <small><i>July 26, 2011</i></small><br />
2887      The <tt>bind</tt> function, can now take an optional third argument, to specify
2888      the <tt>this</tt> of the callback function.
2889      Multiple models with the same <tt>id</tt> are now allowed in a collection.
2890      Fixed a bug where calling <tt>.fetch(jQueryOptions)</tt> could cause an
2891      incorrect URL to be serialized.
2892      Fixed a brief extra route fire before redirect, when degrading from
2893      <tt>pushState</tt>.
2894    </p>
2895
2896    <p>
2897      <b class="header">0.5.1</b> &mdash; <small><i>July 5, 2011</i></small><br />
2898      Cleanups from the 0.5.0 release, to wit: improved transparent upgrades from
2899      hash-based URLs to pushState, and vice-versa. Fixed inconsistency with
2900      non-modified attributes being passed to <tt>Model#initialize</tt>. Reverted
2901      a <b>0.5.0</b> change that would strip leading hashbangs from routes.
2902      Added <tt>contains</tt> as an alias for <tt>includes</tt>.
2903    </p>
2904
2905    <p>
2906      <b class="header">0.5.0</b> &mdash; <small><i>July 1, 2011</i></small><br />
2907      A large number of tiny tweaks and micro bugfixes, best viewed by looking
2908      at <a href="https://github.com/documentcloud/backbone/compare/0.3.3...0.5.0">the commit diff</a>.
2909      HTML5 <tt>pushState</tt> support, enabled by opting-in with:
2910      <tt>Backbone.history.start({pushState: true})</tt>.
2911      <tt>Controller</tt> was renamed to <tt>Router</tt>, for clarity.
2912      <tt>Collection#refresh</tt> was renamed to <tt>Collection#reset</tt> to emphasize
2913      its ability to both reset the collection with new models, as well as empty
2914      out the collection when used with no parameters.
2915      <tt>saveLocation</tt> was replaced with <tt>navigate</tt>.
2916      RESTful persistence methods (save, fetch, etc.) now return the jQuery deferred
2917      object for further success/error chaining and general convenience.
2918      Improved XSS escaping for <tt>Model#escape</tt>.
2919      Added a <tt>urlRoot</tt> option to allow specifying RESTful urls without
2920      the use of a collection.
2921      An error is thrown if <tt>Backbone.history.start</tt> is called multiple times.
2922      <tt>Collection#create</tt> now validates before initializing the new model.
2923      <tt>view.el</tt> can now be a jQuery string lookup.
2924      Backbone Views can now also take an <tt>attributes</tt> parameter.
2925      <tt>Model#defaults</tt> can now be a function as well as a literal attributes
2926      object.
2927    </p>
2928
2929    <p>
2930      <b class="header">0.3.3</b> &mdash; <small><i>Dec 1, 2010</i></small><br />
2931      Backbone.js now supports <a href="http://zeptojs.com">Zepto</a>, alongside
2932      jQuery, as a framework for DOM manipulation and Ajax support.
2933      Implemented <a href="#Model-escape">Model#escape</a>, to efficiently handle
2934      attributes intended for HTML interpolation. When trying to persist a model,
2935      failed requests will now trigger an <tt>"error"</tt> event. The
2936      ubiquitous <tt>options</tt> argument is now passed as the final argument
2937      to all <tt>"change"</tt> events.
2938    </p>
2939
2940    <p>
2941      <b class="header">0.3.2</b> &mdash; <small><i>Nov 23, 2010</i></small><br />
2942      Bugfix for IE7 + iframe-based "hashchange" events. <tt>sync</tt> may now be
2943      overridden on a per-model, or per-collection basis. Fixed recursion error
2944      when calling <tt>save</tt> with no changed attributes, within a
2945      <tt>"change"</tt> event.
2946    </p>
2947
2948    <p>
2949      <b class="header">0.3.1</b> &mdash; <small><i>Nov 15, 2010</i></small><br />
2950      All <tt>"add"</tt> and <tt>"remove"</tt> events are now sent through the
2951      model, so that views can listen for them without having to know about the
2952      collection. Added a <tt>remove</tt> method to <a href="#View">Backbone.View</a>.
2953      <tt>toJSON</tt> is no longer called at all for <tt>'read'</tt> and <tt>'delete'</tt> requests.
2954      Backbone routes are now able to load empty URL fragments.
2955    </p>
2956
2957    <p>
2958      <b class="header">0.3.0</b> &mdash; <small><i>Nov 9, 2010</i></small><br />
2959      Backbone now has <a href="#Controller">Controllers</a> and
2960      <a href="#History">History</a>, for doing client-side routing based on
2961      URL fragments.
2962      Added <tt>emulateHTTP</tt> to provide support for legacy servers that don't
2963      do <tt>PUT</tt> and <tt>DELETE</tt>.
2964      Added <tt>emulateJSON</tt> for servers that can't accept <tt>application/json</tt>
2965      encoded requests.
2966      Added <a href="#Model-clear">Model#clear</a>, which removes all attributes
2967      from a model.
2968      All Backbone classes may now be seamlessly inherited by CoffeeScript classes.
2969    </p>
2970
2971    <p>
2972      <b class="header">0.2.0</b> &mdash; <small><i>Oct 25, 2010</i></small><br />
2973      Instead of requiring server responses to be namespaced under a <tt>model</tt>
2974      key, now you can define your own <a href="#Model-parse">parse</a> method
2975      to convert responses into attributes for Models and Collections.
2976      The old <tt>handleEvents</tt> function is now named
2977      <a href="#View-delegateEvents">delegateEvents</a>, and is automatically
2978      called as part of the View's constructor.
2979      Added a <a href="#Collection-toJSON">toJSON</a> function to Collections.
2980      Added <a href="#Collection-chain">Underscore's chain</a> to Collections.
2981    </p>
2982
2983    <p>
2984      <b class="header">0.1.2</b> &mdash; <small><i>Oct 19, 2010</i></small><br />
2985      Added a <a href="#Model-fetch">Model#fetch</a> method for refreshing the
2986      attributes of single model from the server.
2987      An <tt>error</tt> callback may now be passed to <tt>set</tt> and <tt>save</tt>
2988      as an option, which will be invoked if validation fails, overriding the
2989      <tt>"error"</tt> event.
2990      You can now tell backbone to use the <tt>_method</tt> hack instead of HTTP
2991      methods by setting <tt>Backbone.emulateHTTP = true</tt>.
2992      Existing Model and Collection data is no longer sent up unnecessarily with
2993      <tt>GET</tt> and <tt>DELETE</tt> requests. Added a <tt>rake lint</tt> task.
2994      Backbone is now published as an <a href="http://npmjs.org">NPM</a> module.
2995    </p>
2996
2997    <p>
2998      <b class="header">0.1.1</b> &mdash; <small><i>Oct 14, 2010</i></small><br />
2999      Added a convention for <tt>initialize</tt> functions to be called
3000      upon instance construction, if defined. Documentation tweaks.
3001    </p>
3002
3003    <p>
3004      <b class="header">0.1.0</b> &mdash; <small><i>Oct 13, 2010</i></small><br />
3005      Initial Backbone release.
3006    </p>
3007
3008    <p>
3009      <br />
3010      <a href="http://documentcloud.org/" title="A DocumentCloud Project" style="background:none;">
3011        <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" style="position:relative;left:-10px;" />
3012      </a>
3013    </p>
3014
3015  </div>
3016
3017  <script src="test/vendor/underscore-1.2.4.js"></script>
3018  <script src="test/vendor/jquery-1.7.1.js"></script>
3019  <script src="test/vendor/json2.js"></script>
3020  <script src="backbone.js"></script>
3021
3022  <script>
3023    // Set up the "play" buttons for each runnable code example.
3024    $(function() {
3025      $('.runnable').each(function() {
3026        var code = this;
3027        var button = '<div class="run" title="Run"></div>';
3028        $(button).insertBefore(code).bind('click', function(){
3029          eval($(code).text());
3030        });
3031      });
3032    });
3033  </script>
3034
3035</body>
3036</html>