PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

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

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