PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

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

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