PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/index.html

https://github.com/anushr/backbone
HTML | 3518 lines | 3044 code | 474 blank | 0 comment | 0 complexity | 392c4078188549fa6d0c2a674e0f5163 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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. <link rel="icon" href="docs/images/favicon.ico" />
  7. <title>Backbone.js</title>
  8. <style>
  9. body {
  10. font-size: 14px;
  11. line-height: 22px;
  12. font-family: Helvetica Neue, Helvetica, Arial;
  13. background: #f4f4f4 url(docs/images/background.png);
  14. }
  15. .interface {
  16. font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
  17. }
  18. div#sidebar {
  19. background: #fff;
  20. position: fixed;
  21. top: 0; left: 0; bottom: 0;
  22. width: 200px;
  23. overflow-y: auto;
  24. overflow-x: hidden;
  25. padding: 15px 0 30px 30px;
  26. border-right: 1px solid #bbb;
  27. box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
  28. }
  29. a.toc_title, a.toc_title:visited {
  30. display: block;
  31. color: black;
  32. font-weight: bold;
  33. margin-top: 15px;
  34. }
  35. a.toc_title:hover {
  36. text-decoration: underline;
  37. }
  38. #sidebar .version {
  39. font-size: 10px;
  40. font-weight: normal;
  41. }
  42. ul.toc_section {
  43. font-size: 11px;
  44. line-height: 14px;
  45. margin: 5px 0 0 0;
  46. padding-left: 0px;
  47. list-style-type: none;
  48. font-family: Lucida Grande;
  49. }
  50. .toc_section li {
  51. cursor: pointer;
  52. margin: 0 0 3px 0;
  53. }
  54. .toc_section li a {
  55. text-decoration: none;
  56. color: black;
  57. }
  58. .toc_section li a:hover {
  59. text-decoration: underline;
  60. }
  61. div.container {
  62. position: relative;
  63. width: 550px;
  64. margin: 40px 0 50px 260px;
  65. }
  66. div.run {
  67. position: absolute;
  68. right: 15px;
  69. width: 26px; height: 18px;
  70. background: url('docs/images/arrows.png') no-repeat -26px 0;
  71. }
  72. div.run:active {
  73. background-position: -51px 0;
  74. }
  75. p, div.container ul {
  76. margin: 25px 0;
  77. width: 550px;
  78. }
  79. p.warning {
  80. font-size: 12px;
  81. line-height: 18px;
  82. font-style: italic;
  83. }
  84. div.container ul {
  85. list-style: circle;
  86. padding-left: 15px;
  87. font-size: 13px;
  88. line-height: 18px;
  89. }
  90. div.container ul li {
  91. margin-bottom: 10px;
  92. }
  93. div.container ul.small {
  94. font-size: 12px;
  95. }
  96. a, a:visited {
  97. color: #444;
  98. }
  99. a:active, a:hover {
  100. color: #000;
  101. }
  102. a.punch {
  103. display: inline-block;
  104. background: #4162a8;
  105. border-top: 1px solid #38538c;
  106. border-right: 1px solid #1f2d4d;
  107. border-bottom: 1px solid #151e33;
  108. border-left: 1px solid #1f2d4d;
  109. -webkit-border-radius: 4px;
  110. -moz-border-radius: 4px;
  111. -ms-border-radius: 4px;
  112. -o-border-radius: 4px;
  113. border-radius: 4px;
  114. -webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  115. -moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  116. -ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  117. -o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  118. box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  119. color: #fff;
  120. font: bold 14px "helvetica neue", helvetica, arial, sans-serif;
  121. line-height: 1;
  122. margin-bottom: 15px;
  123. padding: 8px 0 10px 0;
  124. text-align: center;
  125. text-shadow: 0px -1px 1px #1e2d4d;
  126. text-decoration: none;
  127. width: 225px;
  128. -webkit-background-clip: padding-box; }
  129. a.punch:hover {
  130. -webkit-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  131. -moz-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  132. -ms-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  133. -o-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  134. box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  135. cursor: pointer; }
  136. a.punch:active {
  137. -webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  138. -moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  139. -ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  140. -o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  141. box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  142. margin-top: 5px; margin-bottom: 10px }
  143. a img {
  144. border: 0;
  145. }
  146. h1, h2, h3, h4, h5, h6 {
  147. padding-top: 20px;
  148. }
  149. h2 {
  150. font-size: 22px;
  151. }
  152. b.header {
  153. font-size: 18px;
  154. line-height: 35px;
  155. }
  156. span.alias {
  157. font-size: 14px;
  158. font-style: italic;
  159. margin-left: 20px;
  160. }
  161. table {
  162. margin: 15px 0 0; padding: 0;
  163. }
  164. tr, td {
  165. margin: 0; padding: 0;
  166. }
  167. td {
  168. padding: 0px 15px 5px 0;
  169. }
  170. code, pre, tt {
  171. font-family: Monaco, Consolas, "Lucida Console", monospace;
  172. font-size: 12px;
  173. line-height: 18px;
  174. font-style: normal;
  175. }
  176. tt {
  177. padding: 0px 3px;
  178. background: #fff;
  179. border: 1px solid #ddd;
  180. zoom: 1;
  181. }
  182. code {
  183. margin-left: 20px;
  184. }
  185. pre {
  186. font-size: 12px;
  187. padding: 2px 0 2px 15px;
  188. border: 4px solid #bbb; border-top: 0; border-bottom: 0;
  189. margin: 0px 0 25px;
  190. }
  191. img.example_image {
  192. margin: 0px auto;
  193. }
  194. </style>
  195. </head>
  196. <body>
  197. <div id="sidebar" class="interface">
  198. <a class="toc_title" href="#">
  199. Backbone.js <span class="version">(0.9.1)</span>
  200. </a>
  201. <ul class="toc_section">
  202. <li> <a href="http://github.com/documentcloud/backbone">github repo</a></li>
  203. <li> <a href="docs/backbone.html">annotated source</a></li>
  204. </ul>
  205. <a class="toc_title" href="#introduction">
  206. Introduction
  207. </a>
  208. <a class="toc_title" href="#upgrading">
  209. Upgrading
  210. </a>
  211. <a class="toc_title" href="#Events">
  212. Events
  213. </a>
  214. <ul class="toc_section">
  215. <li> <a href="#Events-on">on</a></li>
  216. <li> <a href="#Events-off">off</a></li>
  217. <li> <a href="#Events-trigger">trigger</a></li>
  218. </ul>
  219. <a class="toc_title" href="#Model">
  220. Model
  221. </a>
  222. <ul class="toc_section">
  223. <li> <a href="#Model-extend">extend</a></li>
  224. <li> <a href="#Model-constructor">constructor / initialize</a></li>
  225. <li> <a href="#Model-get">get</a></li>
  226. <li> <a href="#Model-set">set</a></li>
  227. <li> <a href="#Model-escape">escape</a></li>
  228. <li> <a href="#Model-has">has</a></li>
  229. <li> <a href="#Model-unset">unset</a></li>
  230. <li> <a href="#Model-clear">clear</a></li>
  231. <li> <a href="#Model-id">id</a></li>
  232. <li> <a href="#Model-idAttribute">idAttribute</a></li>
  233. <li> <a href="#Model-cid">cid</a></li>
  234. <li> <a href="#Model-attributes">attributes</a></li>
  235. <li> <a href="#Model-changed">changed</a></li>
  236. <li> <a href="#Model-defaults">defaults</a></li>
  237. <li> <a href="#Model-toJSON">toJSON</a></li>
  238. <li> <a href="#Model-fetch">fetch</a></li>
  239. <li> <a href="#Model-save">save</a></li>
  240. <li> <a href="#Model-destroy">destroy</a></li>
  241. <li> <a href="#Model-validate">validate</a></li>
  242. <li> <a href="#Model-isValid">isValid</a></li>
  243. <li> <a href="#Model-url">url</a></li>
  244. <li> <a href="#Model-urlRoot">urlRoot</a></li>
  245. <li> <a href="#Model-parse">parse</a></li>
  246. <li> <a href="#Model-clone">clone</a></li>
  247. <li> <a href="#Model-isNew">isNew</a></li>
  248. <li> <a href="#Model-change">change</a></li>
  249. <li> <a href="#Model-hasChanged">hasChanged</a></li>
  250. <li> <a href="#Model-changedAttributes">changedAttributes</a></li>
  251. <li> <a href="#Model-previous">previous</a></li>
  252. <li> <a href="#Model-previousAttributes">previousAttributes</a></li>
  253. </ul>
  254. <a class="toc_title" href="#Collection">
  255. Collection
  256. </a>
  257. <ul class="toc_section">
  258. <li> <a href="#Collection-extend">extend</a></li>
  259. <li> <a href="#Collection-model">model</a></li>
  260. <li> <a href="#Collection-constructor">constructor / initialize</a></li>
  261. <li> <a href="#Collection-models">models</a></li>
  262. <li> <a href="#Collection-toJSON">toJSON</a></li>
  263. <li> <a href="#Collection-Underscore-Methods"><b>Underscore Methods (28)</b></a></li>
  264. <li> <a href="#Collection-add">add</a></li>
  265. <li> <a href="#Collection-remove">remove</a></li>
  266. <li> <a href="#Collection-get">get</a></li>
  267. <li> <a href="#Collection-getByCid">getByCid</a></li>
  268. <li> <a href="#Collection-at">at</a></li>
  269. <li> <a href="#Collection-length">length</a></li>
  270. <li> <a href="#Collection-comparator">comparator</a></li>
  271. <li> <a href="#Collection-sort">sort</a></li>
  272. <li> <a href="#Collection-pluck">pluck</a></li>
  273. <li> <a href="#Collection-url">url</a></li>
  274. <li> <a href="#Collection-parse">parse</a></li>
  275. <li> <a href="#Collection-fetch">fetch</a></li>
  276. <li> <a href="#Collection-reset">reset</a></li>
  277. <li> <a href="#Collection-create">create</a></li>
  278. </ul>
  279. <a class="toc_title" href="#Router">
  280. Router
  281. </a>
  282. <ul class="toc_section">
  283. <li> <a href="#Router-extend">extend</a></li>
  284. <li> <a href="#Router-routes">routes</a></li>
  285. <li> <a href="#Router-constructor">constructor / initialize</a></li>
  286. <li> <a href="#Router-route">route</a></li>
  287. <li> <a href="#Router-navigate">navigate</a></li>
  288. </ul>
  289. <a class="toc_title" href="#History">
  290. History
  291. </a>
  292. <ul class="toc_section">
  293. <li> <a href="#History-start">start</a></li>
  294. </ul>
  295. <a class="toc_title" href="#Sync">
  296. Sync
  297. </a>
  298. <ul class="toc_section">
  299. <li> <a href="#Sync">Backbone.sync</a></li>
  300. <li> <a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
  301. <li> <a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
  302. </ul>
  303. <a class="toc_title" href="#View">
  304. View
  305. </a>
  306. <ul class="toc_section">
  307. <li> <a href="#View-extend">extend</a></li>
  308. <li> <a href="#View-constructor">constructor / initialize</a></li>
  309. <li> <a href="#View-el">el</a></li>
  310. <li> <a href="#View-$el">$el</a></li>
  311. <li> <a href="#View-setElement">setElement</a></li>
  312. <li> <a href="#View-attributes">attributes</a></li>
  313. <li> <a href="#View-dollar">$ (jQuery or Zepto)</a></li>
  314. <li> <a href="#View-render">render</a></li>
  315. <li> <a href="#View-remove">remove</a></li>
  316. <li> <a href="#View-make">make</a></li>
  317. <li> <a href="#View-delegateEvents">delegateEvents</a></li>
  318. <li> <a href="#View-undelegateEvents">undelegateEvents</a></li>
  319. </ul>
  320. <a class="toc_title" href="#Utility">
  321. Utility
  322. </a>
  323. <ul class="toc_section">
  324. <li> <a href="#Utility-noConflict">noConflict</a></li>
  325. <li> <a href="#Utility-setDomLibrary">setDomLibrary</a></li>
  326. </ul>
  327. <a class="toc_title" href="#examples">
  328. Examples
  329. </a>
  330. <ul class="toc_section">
  331. <li> <a href="#examples-todos">Todos</a></li>
  332. <li> <a href="#examples-documentcloud">DocumentCloud</a></li>
  333. <li> <a href="#examples-linkedin">LinkedIn Mobile</a></li>
  334. <li> <a href="#examples-flow">Flow</a></li>
  335. <li> <a href="#examples-audiovroom">AudioVroom</a></li>
  336. <li> <a href="#examples-foursquare">Foursquare</a></li>
  337. <li> <a href="#examples-wunderkit">Wunderkit</a></li>
  338. <li> <a href="#examples-khan-academy">Khan Academy</a></li>
  339. <li> <a href="#examples-do">Do</a></li>
  340. <li> <a href="#examples-posterous">Posterous Spaces</a></li>
  341. <li> <a href="#examples-groupon">Groupon Now!</a></li>
  342. <li> <a href="#examples-basecamp">Basecamp Mobile</a></li>
  343. <li> <a href="#examples-slavery-footprint">Slavery Footprint</a></li>
  344. <li> <a href="#examples-stripe">Stripe</a></li>
  345. <li> <a href="#examples-diaspora">Diaspora</a></li>
  346. <li> <a href="#examples-soundcloud">SoundCloud Mobile</a></li>
  347. <li> <a href="#examples-pandora">Pandora</a></li>
  348. <li> <a href="#examples-code-school">Code School</a></li>
  349. <li> <a href="#examples-cloudapp">CloudApp</a></li>
  350. <li> <a href="#examples-seatgeek">SeatGeek</a></li>
  351. <li> <a href="#examples-grove">Grove.io</a></li>
  352. <li> <a href="#examples-kicksend">Kicksend</a></li>
  353. <li> <a href="#examples-shortmail">Shortmail</a></li>
  354. <li> <a href="#examples-battlefield">Battlefield Play4Free</a></li>
  355. <li> <a href="#examples-salon">Salon.io</a></li>
  356. <li> <a href="#examples-tilemill">TileMill</a></li>
  357. <li> <a href="#examples-blossom">Blossom</a></li>
  358. <li> <a href="#examples-animoto">Animoto</a></li>
  359. <li> <a href="#examples-codiqa">Codiqa</a></li>
  360. <li> <a href="#examples-attictv">AtticTV</a></li>
  361. <li> <a href="#examples-decide">Decide</a></li>
  362. <li> <a href="#examples-trello">Trello</a></li>
  363. <li> <a href="#examples-ducksboard">Ducksboard</a></li>
  364. <li> <a href="#examples-picklive">Picklive</a></li>
  365. <li> <a href="#examples-quietwrite">QuietWrite</a></li>
  366. <li> <a href="#examples-tzigla">Tzigla</a></li>
  367. </ul>
  368. <a class="toc_title" href="#faq">
  369. F.A.Q.
  370. </a>
  371. <ul class="toc_section">
  372. <li> <a href="#FAQ-events">Catalog of Events</a></li>
  373. <li> <a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
  374. <li> <a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
  375. <li> <a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
  376. <li> <a href="#FAQ-extending">Extending Backbone</a></li>
  377. <li> <a href="#FAQ-mvc">Traditional MVC</a></li>
  378. <li> <a href="#FAQ-this">Binding "this"</a></li>
  379. <li> <a href="#FAQ-rails">Working with Rails</a></li>
  380. </ul>
  381. <a class="toc_title" href="#changelog">
  382. Change Log
  383. </a>
  384. </div>
  385. <div class="container">
  386. <p>
  387. <img style="width: 451px; height: 80px;" src="docs/images/backbone.png" alt="Backbone.js" />
  388. </p>
  389. <p>
  390. Backbone.js gives structure to web applications
  391. by providing <b>models</b> with key-value binding and custom events,
  392. <b>collections</b> with a rich API of enumerable functions,
  393. <b>views</b> with declarative event handling, and connects it all to your
  394. existing API over a RESTful JSON interface.
  395. </p>
  396. <p>
  397. The project is <a href="http://github.com/documentcloud/backbone/">hosted on GitHub</a>,
  398. and the <a href="docs/backbone.html">annotated source code</a> is available,
  399. as well as an online <a href="test/test.html">test suite</a>,
  400. an <a href="examples/todos/index.html">example application</a>,
  401. a <a href="https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>
  402. and a <a href="#examples">long list of real-world projects</a> that use Backbone.
  403. Backbone is available for use under the <a href="http://github.com/documentcloud/backbone/blob/master/LICENSE">MIT software license</a>.
  404. </p>
  405. <p>
  406. You can report bugs and discuss features on the
  407. <a href="http://github.com/documentcloud/backbone/issues">GitHub issues page</a>,
  408. on Freenode IRC in the <tt>#documentcloud</tt> channel, post questions to the
  409. <a href="https://groups.google.com/forum/#!forum/backbonejs">Google Group</a>,
  410. add pages to the <a href="https://github.com/documentcloud/backbone/wiki">wiki</a>
  411. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  412. </p>
  413. <p>
  414. <i>
  415. Backbone is an open-source component of
  416. <a href="http://documentcloud.org/">DocumentCloud</a>.
  417. </i>
  418. </p>
  419. <h2 id="downloads">
  420. Downloads &amp; Dependencies
  421. <span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
  422. </h2>
  423. <table>
  424. <tr>
  425. <td><a class="punch" href="backbone.js">Development Version (0.9.1)</a></td>
  426. <td><i>47kb, Full source, lots of comments</i></td>
  427. </tr>
  428. <tr>
  429. <td><a class="punch" href="backbone-min.js">Production Version (0.9.1)</a></td>
  430. <td><i>5.3kb, Packed and gzipped</i></td>
  431. </tr>
  432. </table>
  433. <p>
  434. Backbone's only hard dependency is
  435. <a href="http://documentcloud.github.com/underscore/">Underscore.js</a> <small>( > 1.3.1)</small>.
  436. For RESTful persistence, history support via <a href="#Router">Backbone.Router</a>
  437. and DOM manipulation with <a href="#View">Backbone.View</a>, include
  438. <a href="https://github.com/douglascrockford/JSON-js">json2.js</a>, and either
  439. <a href="http://jquery.com">jQuery</a> <small>( > 1.4.2)</small> or
  440. <a href="http://zeptojs.com/">Zepto</a>.
  441. </p>
  442. <h2 id="introduction">Introduction</h2>
  443. <p>
  444. When working on a web application that involves a lot of JavaScript, one
  445. of the first things you learn is to stop tying your data to the DOM. It's all
  446. too easy to create JavaScript applications that end up as tangled piles of
  447. jQuery selectors and callbacks, all trying frantically to keep data in
  448. sync between the HTML UI, your JavaScript logic, and the database on your
  449. server. For rich client-side applications, a more structured approach
  450. is often helpful.
  451. </p>
  452. <p>
  453. With Backbone, you represent your data as
  454. <a href="#Model">Models</a>, which can be created, validated, destroyed,
  455. and saved to the server. Whenever a UI action causes an attribute of
  456. a model to change, the model triggers a <i>"change"</i> event; all
  457. the <a href="#View">Views</a> that display the model's state can be notified of the
  458. change, so that they are able to respond accordingly, re-rendering themselves with
  459. the new information. In a finished Backbone app, you don't have to write the glue
  460. code that looks into the DOM to find an element with a specific <i>id</i>,
  461. and update the HTML manually
  462. &mdash; when the model changes, the views simply update themselves.
  463. </p>
  464. <p>
  465. If you're new here, and aren't yet quite sure what Backbone is for, start by
  466. browsing the <a href="#examples">list of Backbone-based projects</a>.
  467. </p>
  468. <p>
  469. Many of the examples that follow are runnable. Click the <i>play</i> button
  470. to execute them.
  471. </p>
  472. <h2 id="upgrading">Upgrading to 0.9</h2>
  473. <p>
  474. Backbone's <b>0.9</b> series should be considered as a release candidate
  475. for an upcoming <b>1.0</b>. Some APIs have changed, and while there is a
  476. <a href="#changelog">change log</a> available, and many new features to
  477. take advantage of, there are a few specific changes where you'll need
  478. to take care:
  479. </p>
  480. <ul>
  481. <li>
  482. If you've ever manually set <tt>this.el</tt> in a Backbone View to be a
  483. particular DOM element, you'll want to use
  484. <a href="#View-setElement">setElement</a> instead.
  485. </li>
  486. <li>
  487. Creating and destroying models is now optimistic. Pass <tt>{wait: true}</tt>
  488. if you need the previous behavior of waiting for the server to acknowledge
  489. success. You can now also pass <tt>{wait: true}</tt> to <a href="#Model-save">save</a> calls.
  490. </li>
  491. <li>
  492. If you have been writing a fair amount of <tt>$(view.el)</tt>, there's now
  493. a cached reference for that jQuery object: <a href="#View-$el">$el</a>.
  494. </li>
  495. <li>
  496. If you're upgrading, make sure you also upgrade your version of Underscore.js
  497. to the latest &mdash; 1.3.1 or greater.
  498. </li>
  499. <li>
  500. <tt>model.set</tt> will no longer trigger change events when setting a value
  501. with <tt>{silent: true}</tt> then setting it back to its original value.
  502. Similarly, after changing an attribute silently, that <tt>change:attribute</tt>
  503. event <i>will</i> fire during the next change.
  504. </li>
  505. <li>
  506. Since <tt>view.$(selector)</tt> is now equivalent to <tt>view.$el.find(selector)</tt>
  507. rather than <tt>$(selector, view.el)</tt> it can no longer be used when
  508. <tt>selector</tt> is an HTML string or DOM element.
  509. </li>
  510. </ul>
  511. <h2 id="Events">Backbone.Events</h2>
  512. <p>
  513. <b>Events</b> is a module that can be mixed in to any object, giving the
  514. object the ability to bind and trigger custom named events. Events do not
  515. have to be declared before they are bound, and may take passed arguments.
  516. For example:
  517. </p>
  518. <pre class="runnable">
  519. var object = {};
  520. _.extend(object, Backbone.Events);
  521. object.on("alert", function(msg) {
  522. alert("Triggered " + msg);
  523. });
  524. object.trigger("alert", "an event");
  525. </pre>
  526. <p>
  527. For example, to make a handy event dispatcher that can coordinate events
  528. among different areas of your application: <tt>var dispatcher = _.clone(Backbone.Events)</tt>
  529. </p>
  530. <p id="Events-on">
  531. <b class="header">on</b><code>object.on(event, callback, [context])</code><span class="alias">Alias: bind</span>
  532. <br />
  533. Bind a <b>callback</b> function to an object. The callback will be invoked
  534. whenever the <b>event</b> is fired.
  535. If you have a large number of different events on a page, the convention is to use colons to
  536. namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>.
  537. The event string may also be a space-delimited list of several events...
  538. </p>
  539. <pre>
  540. book.on("change:title change:author", ...);
  541. </pre>
  542. <p>
  543. To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
  544. pass the optional third argument: <tt>model.on('change', this.render, this)</tt>
  545. </p>
  546. <p>
  547. Callbacks bound to the special
  548. <tt>"all"</tt> event will be triggered when any event occurs, and are passed
  549. the name of the event as the first argument. For example, to proxy all events
  550. from one object to another:
  551. </p>
  552. <pre>
  553. proxy.on("all", function(eventName) {
  554. object.trigger(eventName);
  555. });
  556. </pre>
  557. <p id="Events-off">
  558. <b class="header">off</b><code>object.off([event], [callback], [context])</code><span class="alias">Alias: unbind</span>
  559. <br />
  560. Remove a previously-bound <b>callback</b> function from an object. If no
  561. <b>context</b> is specified, all of the versions of the callback with
  562. different contexts will be removed. If no
  563. callback is specified, all callbacks for the <b>event</b> will be
  564. removed. If no event is specified, <i>all</i> event callbacks on the object
  565. will be removed.
  566. </p>
  567. <pre>
  568. object.off("change", onChange); // Removes just the onChange callback.
  569. object.off("change"); // Removes all "change" callbacks.
  570. object.off(); // Removes all callbacks on object.
  571. </pre>
  572. <p id="Events-trigger">
  573. <b class="header">trigger</b><code>object.trigger(event, [*args])</code>
  574. <br />
  575. Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
  576. Subsequent arguments to <b>trigger</b> will be passed along to the
  577. event callbacks.
  578. </p>
  579. <h2 id="Model">Backbone.Model</h2>
  580. <p>
  581. <b>Models</b> are the heart of any JavaScript application, containing
  582. the interactive data as well as a large part of the logic surrounding it:
  583. conversions, validations, computed properties, and access control. You
  584. extend <b>Backbone.Model</b> with your domain-specific methods, and
  585. <b>Model</b> provides a basic set of functionality for managing changes.
  586. </p>
  587. <p>
  588. The following is a contrived example, but it demonstrates defining a model
  589. with a custom method, setting an attribute, and firing an event keyed
  590. to changes in that specific attribute.
  591. After running this code once, <tt>sidebar</tt> will be
  592. available in your browser's console, so you can play around with it.
  593. </p>
  594. <pre class="runnable">
  595. var Sidebar = Backbone.Model.extend({
  596. promptColor: function() {
  597. var cssColor = prompt("Please enter a CSS color:");
  598. this.set({color: cssColor});
  599. }
  600. });
  601. window.sidebar = new Sidebar;
  602. sidebar.on('change:color', function(model, color) {
  603. $('#sidebar').css({background: color});
  604. });
  605. sidebar.set({color: 'white'});
  606. sidebar.promptColor();
  607. </pre>
  608. <p id="Model-extend">
  609. <b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
  610. <br />
  611. To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
  612. and provide instance <b>properties</b>, as well as optional
  613. <b>classProperties</b> to be attached directly to the constructor function.
  614. </p>
  615. <p>
  616. <b>extend</b> correctly sets up the prototype chain, so subclasses created
  617. with <b>extend</b> can be further extended and subclassed as far as you like.
  618. </p>
  619. <pre>
  620. var Note = Backbone.Model.extend({
  621. initialize: function() { ... },
  622. author: function() { ... },
  623. coordinates: function() { ... },
  624. allowedToEdit: function(account) {
  625. return true;
  626. }
  627. });
  628. var PrivateNote = Note.extend({
  629. allowedToEdit: function(account) {
  630. return account.owns(this);
  631. }
  632. });
  633. </pre>
  634. <p class="warning">
  635. Brief aside on <tt>super</tt>: JavaScript does not provide
  636. a simple way to call super &mdash; the function of the same name defined
  637. higher on the prototype chain. If you override a core function like
  638. <tt>set</tt>, or <tt>save</tt>, and you want to invoke the
  639. parent object's implementation, you'll have to explicitly call it, along these lines:
  640. </p>
  641. <pre>
  642. var Note = Backbone.Model.extend({
  643. set: function(attributes, options) {
  644. Backbone.Model.prototype.set.call(this, attributes, options);
  645. ...
  646. }
  647. });
  648. </pre>
  649. <p id="Model-constructor">
  650. <b class="header">constructor / initialize</b><code>new Model([attributes])</code>
  651. <br />
  652. When creating an instance of a model, you can pass in the initial values
  653. of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
  654. model. If you define an <b>initialize</b> function, it will be invoked when
  655. the model is created.
  656. </p>
  657. <pre>
  658. new Book({
  659. title: "One Thousand and One Nights",
  660. author: "Scheherazade"
  661. });
  662. </pre>
  663. <p>
  664. In rare cases, if you're looking to get fancy,
  665. you may want to override <b>constructor</b>, which allows
  666. you to replace the actual constructor function for your model.
  667. </p>
  668. <p id="Model-get">
  669. <b class="header">get</b><code>model.get(attribute)</code>
  670. <br />
  671. Get the current value of an attribute from the model. For example:
  672. <tt>note.get("title")</tt>
  673. </p>
  674. <p id="Model-set">
  675. <b class="header">set</b><code>model.set(attributes, [options])</code>
  676. <br />
  677. Set a hash of attributes (one or many) on the model. If any of the attributes
  678. change the models state, a <tt>"change"</tt> event will be triggered, unless
  679. <tt>{silent: true}</tt> is passed as an option. Change events for specific
  680. attributes are also triggered, and you can bind to those as well, for example:
  681. <tt>change:title</tt>, and <tt>change:content</tt>. You may also pass
  682. individual keys and values.
  683. </p>
  684. <pre>
  685. note.set({title: "March 20", content: "In his eyes she eclipses..."});
  686. book.set("title", "A Scandal in Bohemia");
  687. </pre>
  688. <p>
  689. If the model has a <a href="#Model-validate">validate</a> method,
  690. it will be validated before the attributes are set, no changes will
  691. occur if the validation fails, and <b>set</b> will return <tt>false</tt>.
  692. Otherwise, <b>set</b> returns a reference to the model.
  693. You may also pass an <tt>error</tt>
  694. callback in the options, which will be invoked instead of triggering an
  695. <tt>"error"</tt> event, should validation fail.
  696. </p>
  697. <p id="Model-escape">
  698. <b class="header">escape</b><code>model.escape(attribute)</code>
  699. <br />
  700. Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
  701. of a model's attribute. If you're interpolating data from the model into
  702. HTML, using <b>escape</b> to retrieve attributes will prevent
  703. <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
  704. </p>
  705. <pre class="runnable">
  706. var hacker = new Backbone.Model({
  707. name: "&lt;script&gt;alert('xss')&lt;/script&gt;"
  708. });
  709. alert(hacker.escape('name'));
  710. </pre>
  711. <p id="Model-has">
  712. <b class="header">has</b><code>model.has(attribute)</code>
  713. <br />
  714. Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
  715. value.
  716. </p>
  717. <pre>
  718. if (note.has("title")) {
  719. ...
  720. }
  721. </pre>
  722. <p id="Model-unset">
  723. <b class="header">unset</b><code>model.unset(attribute, [options])</code>
  724. <br />
  725. Remove an attribute by deleting it from the internal attributes hash.
  726. Fires a <tt>"change"</tt> event unless <tt>silent</tt> is passed as an option.
  727. </p>
  728. <p id="Model-clear">
  729. <b class="header">clear</b><code>model.clear([options])</code>
  730. <br />
  731. Removes all attributes from the model. Fires a <tt>"change"</tt> event unless
  732. <tt>silent</tt> is passed as an option.
  733. </p>
  734. <p id="Model-id">
  735. <b class="header">id</b><code>model.id</code>
  736. <br />
  737. A special property of models, the <b>id</b> is an arbitrary string
  738. (integer id or UUID). If you set the <b>id</b> in the
  739. attributes hash, it will be copied onto the model as a direct property.
  740. Models can be retrieved by id from collections, and the id is used to generate
  741. model URLs by default.
  742. </p>
  743. <p id="Model-idAttribute">
  744. <b class="header">idAttribute</b><code>model.idAttribute</code>
  745. <br />
  746. A model's unique identifier is stored under the <tt>id</tt> attribute.
  747. If you're directly communicating with a backend (CouchDB, MongoDB) that uses
  748. a different unique key, you may set a Model's <tt>idAttribute</tt> to
  749. transparently map from that key to <tt>id</tt>.
  750. <pre class="runnable">
  751. var Meal = Backbone.Model.extend({
  752. idAttribute: "_id"
  753. });
  754. var cake = new Meal({ _id: 1, name: "Cake" });
  755. alert("Cake id: " + cake.id);
  756. </pre>
  757. </p>
  758. <p id="Model-cid">
  759. <b class="header">cid</b><code>model.cid</code>
  760. <br />
  761. A special property of models, the <b>cid</b> or client id is a unique identifier
  762. automatically assigned to all models when they're first created. Client ids
  763. are handy when the model has not yet been saved to the server, and does not
  764. yet have its eventual true <b>id</b>, but already needs to be visible in the UI.
  765. Client ids take the form: <tt>c1, c2, c3 ...</tt>
  766. </p>
  767. <p id="Model-attributes">
  768. <b class="header">attributes</b><code>model.attributes</code>
  769. <br />
  770. The <b>attributes</b> property is the internal hash containing the model's
  771. state. Please use <a href="#Model-set">set</a> to update the attributes instead of modifying
  772. them directly. If you'd like to retrieve and munge a copy of the model's
  773. attributes, use <a href="#Model-toJSON">toJSON</a> instead.
  774. </p>
  775. <p id="Model-changed">
  776. <b class="header">changed</b><code>model.changed</code>
  777. <br />
  778. The <b>changed</b> property is the internal hash containing all the attributes
  779. that have changed since the last <tt>"change"</tt> event was triggered.
  780. Please do not update <b>changed</b> directly. Its state is maintained internally
  781. by <a href="#Model-set">set</a> and <a href="#Model-change">change</a>.
  782. A copy of <b>changed</b> can be acquired from
  783. <a href="#Model-changedAttributes">changedAttributes</a>.
  784. </p>
  785. <p id="Model-defaults">
  786. <b class="header">defaults</b><code>model.defaults or model.defaults()</code>
  787. <br />
  788. The <b>defaults</b> hash (or function) can be used to specify the default
  789. attributes for your model. When creating an instance of the model,
  790. any unspecified attributes will be set to their default value.
  791. </p>
  792. <pre class="runnable">
  793. var Meal = Backbone.Model.extend({
  794. defaults: {
  795. "appetizer": "caesar salad",
  796. "entree": "ravioli",
  797. "dessert": "cheesecake"
  798. }
  799. });
  800. alert("Dessert will be " + (new Meal).get('dessert'));
  801. </pre>
  802. <p class="warning">
  803. Remember that in JavaScript, objects are passed by reference, so if you
  804. include an object as a default value, it will be shared among all instances.
  805. </p>
  806. <p id="Model-toJSON">
  807. <b class="header">toJSON</b><code>model.toJSON()</code>
  808. <br />
  809. Return a copy of the model's <a href="#Model-attributes">attributes</a> for JSON stringification.
  810. This can be used for persistence, serialization, or for augmentation before
  811. being handed off to a view. The name of this method is a bit confusing, as
  812. it doesn't actually return a JSON string &mdash; but I'm afraid that it's
  813. the way that the <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript API for <b>JSON.stringify</b> works</a>.
  814. </p>
  815. <pre class="runnable">
  816. var artist = new Backbone.Model({
  817. firstName: "Wassily",
  818. lastName: "Kandinsky"
  819. });
  820. artist.set({birthday: "December 16, 1866"});
  821. alert(JSON.stringify(artist));
  822. </pre>
  823. <p id="Model-fetch">
  824. <b class="header">fetch</b><code>model.fetch([options])</code>
  825. <br />
  826. Resets the model's state from the server. Useful if the model has never
  827. been populated with data, or if you'd like to ensure that you have the
  828. latest server state. A <tt>"change"</tt> event will be triggered if the
  829. server's state differs from the current attributes. Accepts
  830. <tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
  831. are passed <tt>(model, response)</tt> as arguments.
  832. </p>
  833. <pre>
  834. // Poll every 10 seconds to keep the channel model up-to-date.
  835. setInterval(function() {
  836. channel.fetch();
  837. }, 10000);
  838. </pre>
  839. <p id="Model-save">
  840. <b class="header">save</b><code>model.save([attributes], [options])</code>
  841. <br />
  842. Save a model to your database (or alternative persistence layer),
  843. by delegating to <a href="#Sync">Backbone.sync</a>. The <b>attributes</b>
  844. hash (as in <a href="#Model-set">set</a>) should contain the attributes
  845. you'd like to change &mdash; keys that aren't mentioned won't be altered &mdash; but,
  846. a <i>complete representation</i> of the resource will be sent to the server.
  847. As with <tt>set</tt>, you may pass individual keys and values instead of a hash.
  848. If the model has a <a href="#Model-validate">validate</a>
  849. method, and validation fails, the model will not be saved. If the model
  850. <a href="#Model-isNew">isNew</a>, the save will be a <tt>"create"</tt>
  851. (HTTP <tt>POST</tt>), if the model already
  852. exists on the server, the save will be an <tt>"update"</tt> (HTTP <tt>PUT</tt>).
  853. </p>
  854. <p>
  855. Calling <tt>save</tt> with new attributes will cause a <tt>"change"</tt>
  856. event immediately, and a <tt>"sync"</tt> event after the server has acknowledged
  857. the successful change. Pass <tt>{wait: true}</tt> if you'd like to wait
  858. for the server before setting the new attributes on the model.
  859. </p>
  860. <p>
  861. In the following example, notice how our overridden version
  862. of <tt>Backbone.sync</tt> receives a <tt>"create"</tt> request
  863. the first time the model is saved and an <tt>"update"</tt>
  864. request the second time.
  865. </p>
  866. <pre class="runnable">
  867. Backbone.sync = function(method, model) {
  868. alert(method + ": " + JSON.stringify(model));
  869. model.id = 1;
  870. };
  871. var book = new Backbone.Model({
  872. title: "The Rough Riders",
  873. author: "Theodore Roosevelt"
  874. });
  875. book.save();
  876. book.save({author: "Teddy"});
  877. </pre>
  878. <p>
  879. <b>save</b> accepts <tt>success</tt> and <tt>error</tt> callbacks in the
  880. options hash, which are passed <tt>(model, response)</tt> as arguments.
  881. The <tt>error</tt> callback will also be invoked if the model has a
  882. <tt>validate</tt> method, and validation fails. If a server-side
  883. validation fails, return a non-<tt>200</tt> HTTP response code, along with
  884. an error response in text or JSON.
  885. </p>
  886. <pre>
  887. book.save("author", "F.D.R.", {error: function(){ ... }});
  888. </pre>
  889. <p id="Model-destroy">
  890. <b class="header">destroy</b><code>model.destroy([options])</code>
  891. <br />
  892. Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
  893. request to <a href="#Sync">Backbone.sync</a>. Accepts
  894. <tt>success</tt> and <tt>error</tt> callbacks in the options hash.
  895. Triggers a <tt>"destroy"</tt> event on the model, which will bubble up
  896. through any collections that contain it, and a <tt>"sync"</tt> event, after
  897. the server has successfully acknowledged the model's deletion. Pass
  898. <tt>{wait: true}</tt> if you'd like to wait for the server to respond
  899. before removing the model from the collection.
  900. </p>
  901. <pre>
  902. book.destroy({success: function(model, response) {
  903. ...
  904. }});
  905. </pre>
  906. <p id="Model-validate">
  907. <b class="header">validate</b><code>model.validate(attributes)</code>
  908. <br />
  909. This method is left undefined, and you're encouraged to override it with
  910. your custom validation logic, if you have any that can be performed
  911. in JavaScript. <b>validate</b> is called before <tt>set</tt> and
  912. <tt>save</tt>, and is passed the attributes that are about to be updated.
  913. If the model and attributes are valid, don't return anything from <b>validate</b>;
  914. if the attributes are invalid, return an error of your choosing. It
  915. can be as simple as a string error message to be displayed, or a complete
  916. error object that describes the error programmatically. <tt>set</tt> and
  917. <tt>save</tt> will not continue if <b>validate</b> returns an error.
  918. Failed validations trigger an <tt>"error"</tt> event.
  919. </p>
  920. <pre class="runnable">
  921. var Chapter = Backbone.Model.extend({
  922. validate: function(attrs) {
  923. if (attrs.end < attrs.start) {
  924. return "can't end before it starts";
  925. }
  926. }
  927. });
  928. var one = new Chapter({
  929. title : "Chapter One: The Beginning"
  930. });
  931. one.on("error", function(model, error) {
  932. alert(model.get("title") + " " + error);
  933. });
  934. one.set({
  935. start: 15,
  936. end: 10
  937. });
  938. </pre>
  939. <p>
  940. <tt>"error"</tt> events are useful for providing coarse-grained error
  941. messages at the model or collection level, but if you have a specific view
  942. that can better handle the error, you may override and suppress the event
  943. by passing an <tt>error</tt> callback directly:
  944. </p>
  945. <pre>
  946. account.set({access: "unlimited"}, {
  947. error: function(model, error) {
  948. alert(error);
  949. }
  950. });
  951. </pre>
  952. <p id="Model-isValid">
  953. <b class="header">isValid</b><code>model.isValid()</code>
  954. <br />
  955. Models may enter an invalid state if you make changes to them silently
  956. ... useful when dealing with form input. Call <tt>model.isValid()</tt>
  957. to check if the model is currently in a valid state, according to your
  958. <tt>validate</tt> function.
  959. </p>
  960. <p id="Model-url">
  961. <b class="header">url</b><code>model.url()</code>
  962. <br />
  963. Returns the relative URL where the model's resource would be located on
  964. the server. If your models are located somewhere else, override this method
  965. with the correct logic. Generates URLs of the form: <tt>"/[collection.url]/[id]"</tt>,
  966. falling back to <tt>"/[urlRoot]/id"</tt> if the model is not part of a collection.
  967. </p>
  968. <p>
  969. Delegates to <a href="#Collection-url">Collection#url</a> to generate the
  970. URL, so make sure that you have it defined, or a <a href="#Model-urlRoot">urlRoot</a>
  971. property, if all models of this class share a common root URL.
  972. A model with an id of <tt>101</tt>, stored in a
  973. <a href="#Collection">Backbone.Collection</a> with a <tt>url</tt> of <tt>"/documents/7/notes"</tt>,
  974. would have this URL: <tt>"/documents/7/notes/101"</tt>
  975. </p>
  976. <p id="Model-urlRoot">
  977. <b class="header">urlRoot</b><code>model.urlRoot or model.urlRoot()</code>
  978. <br />
  979. Specify a <tt>urlRoot</tt> if you're using a model outside of a collection,
  980. to enable the default <a href="#Model-url">url</a> function to generate
  981. URLs based on the model id. <tt>"/[urlRoot]/id"</tt><br />
  982. Note that <tt>urlRoot</tt> may also be defined as a function.
  983. </p>
  984. <pre class="runnable">
  985. var Book = Backbone.Model.extend({urlRoot : '/books'});
  986. var solaris = new Book({id: "1083-lem-solaris"});
  987. alert(solaris.url());
  988. </pre>
  989. <p id="Model-parse">
  990. <b class="header">parse</b><code>model.parse(response)</code>
  991. <br />
  992. <b>parse</b> is called whenever a model's data is returned by the
  993. server, in <a href="#Model-fetch">fetch</a>, and <a href="#Model-save">save</a>.
  994. The function is passed the raw <tt>response</tt> object, and should return
  995. the attributes hash to be <a href="#Model-set">set</a> on the model. The
  996. default implementation is a no-op, simply passing through the JSON response.
  997. Override this if you need to work with a preexisting API, or better namespace
  998. your responses.
  999. </p>
  1000. <p>
  1001. If you're working with a Rails backend, you'll notice that Rails' default
  1002. <tt>to_json</tt> implementation includes a model's attributes under a
  1003. namespace. To disable this behavior for seamless Backbone integration, set:
  1004. </p>
  1005. <pre>
  1006. ActiveRecord::Base.include_root_in_json = false
  1007. </pre>
  1008. <p id="Model-clone">
  1009. <b class="header">clone</b><code>model.clone()</code>
  1010. <br />
  1011. Returns a new instance of the model with identical attributes.
  1012. </p>
  1013. <p id="Model-isNew">
  1014. <b class="header">isNew</b><code>model.isNew()</code>
  1015. <br />
  1016. Has this model been saved to the server yet? If the model does not yet have
  1017. an <tt>id</tt>, it is considered to be new.
  1018. </p>
  1019. <p id="Model-change">
  1020. <b class="header">change</b><code>model.change()</code>
  1021. <br />
  1022. Manually trigger the <tt>"change"</tt> event and a <tt>"change:attribute"</tt>
  1023. event for each attribute that has changed. If you've been passing
  1024. <tt>{silent: true}</tt> to the <a href="#Model-set">set</a> function in order to
  1025. aggregate rapid changes to a model, you'll want to call <tt>model.change()</tt>
  1026. when you're all finished.
  1027. </p>
  1028. <p id="Model-hasChanged">
  1029. <b class="header">hasChanged</b><code>model.hasChanged([attribute])</code>
  1030. <br />
  1031. Has the model changed since the last <tt>"change"</tt> event? If an <b>attribute</b>
  1032. is passed, returns <tt>true</tt> if that specific attribute has changed.
  1033. </p>
  1034. <p class="warning">
  1035. Note that this method, and the following change-related ones,
  1036. are only useful during the course of a <tt>"change"</tt> event.
  1037. </p>
  1038. <pre>
  1039. book.on("change", function() {
  1040. if (book.hasChanged("title")) {
  1041. ...
  1042. }
  1043. });
  1044. </pre>
  1045. <p id="Model-changedAttributes">
  1046. <b class="header">changedAttributes</b><code>model.changedAttributes([attributes])</code>
  1047. <br />
  1048. Retrieve a hash of only the model's attributes that have changed. Optionally,
  1049. an external <b>attributes</b> hash can be passed in, returning
  1050. the attributes in that hash which differ from the model. This can be used
  1051. to figure out which portions of a view should be updated, or what calls
  1052. need to be made to sync the changes to the server.
  1053. </p>
  1054. <p id="Model-previous">
  1055. <b class="header">previous</b><code>model.previous(attribute)</code>
  1056. <br />
  1057. During a <tt>"change"</tt> event, this method can be used to get the
  1058. previous value of a changed attribute.
  1059. </p>
  1060. <pre class="runnable">
  1061. var bill = new Backbone.Model({
  1062. name: "Bill Smith"
  1063. });
  1064. bill.on("change:name", function(model, name) {
  1065. alert("Changed name from " + bill.previous("name") + " to " + name);
  1066. });
  1067. bill.set({name : "Bill Jones"});
  1068. </pre>
  1069. <p id="Model-previousAttributes">
  1070. <b class="header">previousAttributes</b><code>model.previousAttributes()</code>
  1071. <br />
  1072. Return a copy of the model's previous attributes. Useful for getting a
  1073. diff between versions of a model, or getting back to a valid state after
  1074. an error occurs.
  1075. </p>
  1076. <h2 id="Collection">Backbone.Collection</h2>
  1077. <p>
  1078. Collections are ordered sets of models. You can bind <tt>"change"</tt> events
  1079. to be notified when any model in the collection has been modified,
  1080. listen for <tt>"add"</tt> and <tt>"remove"</tt> events, <tt>fetch</tt>
  1081. the collection from the server, and use a full suite of
  1082. <a href="#Collection-Underscore-Methods">Underscore.js methods</a>.
  1083. </p>
  1084. <p>
  1085. Any event that is triggered on a model in a collection will also be
  1086. triggered on the collection directly, for convenience.
  1087. This allows you to listen for changes to specific attributes in any
  1088. model in a collection, for example:
  1089. <tt>Documents.on("change:selected", ...)</tt>
  1090. </p>
  1091. <p id="Collection-extend">
  1092. <b class="header">extend</b><code>Backbone.Collection.extend(properties, [classProperties])</code>
  1093. <br />
  1094. To create a <b>Collection</b> class of your own, extend <b>Backbone.Collection</b>,
  1095. providing instance <b>properties</b>, as well as optional <b>classProperties</b> to be attached
  1096. directly to the collection's constructor function.
  1097. </p>
  1098. <p id="Collection-model">
  1099. <b class="header">model</b><code>collection.model</code>
  1100. <br />
  1101. Override this property to specify the model class that the collection
  1102. contains. If defined, you can pass raw attributes objects (and arrays) to
  1103. <a href="#Collection-add">add</a>, <a href="#Collection-create">create</a>,
  1104. and <a href="#Collection-reset">reset</a>, and the attributes will be
  1105. converted into a model of the proper type.
  1106. </p>
  1107. <pre>
  1108. var Library = Backbone.Collection.extend({
  1109. model: Book
  1110. });
  1111. </pre>
  1112. <p id="Collection-constructor">
  1113. <b class="header">constructor / initialize</b><code>new Collection([models], [options])</code>
  1114. <br />
  1115. When creating a Collection, you may choose to pass in the initial array of <b>models</b>.
  1116. The collection's <a href="#Collection-comparator">comparator</a> function
  1117. may be included as an option. If you define an <b>initialize</b> function, it will be
  1118. invoked when the collection is created.
  1119. </p>
  1120. <pre>
  1121. var tabs = new TabSet([tab1, tab2, tab3]);
  1122. </pre>
  1123. <p id="Collection-models">
  1124. <b class="header">models</b><code>collection.models</code>
  1125. <br />
  1126. Raw access to the JavaScript array of models inside of the collection. Usually you'll
  1127. want to use <tt>get</tt>, <tt>at</tt>, or the <b>Underscore methods</b>
  1128. to access model objects, but occasionally a direct reference to the array
  1129. is desired.
  1130. </p>
  1131. <p id="Collection-toJSON">
  1132. <b class="header">toJSON</b><code>collection.toJSON()</code>
  1133. <br />
  1134. Return an array containing the attributes hash of each model in the
  1135. collection. This can be used to serialize and persist the
  1136. collection as a whole. The name of this method is a bit confusing, because
  1137. it conforms to
  1138. <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript's JSON API</a>.
  1139. </p>
  1140. <pre class="runnable">
  1141. var collection = new Backbone.Collection([
  1142. {name: "Tim", age: 5},
  1143. {name: "Ida", age: 26},
  1144. {name: "Rob", age: 55}
  1145. ]);
  1146. alert(JSON.stringify(collection));
  1147. </pre>
  1148. <p id="Collection-Underscore-Methods">
  1149. <b class="header">Underscore Methods (28)</b>
  1150. <br />
  1151. Backbone proxies to <b>Underscore.js</b> to provide 28 iteration functions
  1152. on <b>Backbone.Collection</b>. They aren't all documented here, but
  1153. you can take a look at the Underscore documentation for the full details&hellip;
  1154. </p>
  1155. <ul class="small">
  1156. <li><a href="http://documentcloud.github.com/underscore/#each">forEach (each)</a></li>
  1157. <li><a href="http://documentcloud.github.com/underscore/#map">map</a></li>
  1158. <li><a href="http://documentcloud.github.com/underscore/#reduce">reduce (foldl, inject)</a></li>
  1159. <li><a href="http://documentcloud.github.com/underscore/#reduceRight">reduceRight (foldr)</a></li>
  1160. <li><a href="http://documentcloud.github.com/underscore/#detect">find (detect)</a></li>
  1161. <li><a href="http://documentcloud.github.com/underscore/#select">filter (select)</a></li>
  1162. <li><a href="http://documentcloud.github.com/underscore/#reject">reject</a

Large files files are truncated, but you can click here to view the full file