PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/shortener/static/script/node_modules/backbone/index.html

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

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