PageRenderTime 74ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

https://bitbucket.org/dlisboa/backbone
HTML | 4287 lines | 3754 code | 533 blank | 0 comment | 0 complexity | 61c0d4bb072abbf450d73a35c14241e2 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.9)</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-once">once</a></li>
  303. <li> <a href="#Events-listenTo">listenTo</a></li>
  304. <li> <a href="#Events-stopListening">stopListening</a></li>
  305. <li>- <a href="#Events-catalog"><b>Catalog of Built-in Events</b></a></li>
  306. </ul>
  307. <a class="toc_title" href="#Model">
  308. Model
  309. </a>
  310. <ul class="toc_section">
  311. <li> <a href="#Model-extend">extend</a></li>
  312. <li> <a href="#Model-constructor">constructor / initialize</a></li>
  313. <li> <a href="#Model-get">get</a></li>
  314. <li> <a href="#Model-set">set</a></li>
  315. <li> <a href="#Model-escape">escape</a></li>
  316. <li> <a href="#Model-has">has</a></li>
  317. <li> <a href="#Model-unset">unset</a></li>
  318. <li> <a href="#Model-clear">clear</a></li>
  319. <li> <a href="#Model-id">id</a></li>
  320. <li> <a href="#Model-idAttribute">idAttribute</a></li>
  321. <li> <a href="#Model-cid">cid</a></li>
  322. <li> <a href="#Model-attributes">attributes</a></li>
  323. <li> <a href="#Model-changed">changed</a></li>
  324. <li> <a href="#Model-defaults">defaults</a></li>
  325. <li> <a href="#Model-toJSON">toJSON</a></li>
  326. <li> <a href="#Model-sync">sync</a></li>
  327. <li> <a href="#Model-fetch">fetch</a></li>
  328. <li> <a href="#Model-save">save</a></li>
  329. <li> <a href="#Model-destroy">destroy</a></li>
  330. <li> <a href="#Model-validate">validate</a></li>
  331. <li> <a href="#Model-url">url</a></li>
  332. <li> <a href="#Model-urlRoot">urlRoot</a></li>
  333. <li> <a href="#Model-parse">parse</a></li>
  334. <li> <a href="#Model-clone">clone</a></li>
  335. <li> <a href="#Model-isNew">isNew</a></li>
  336. <li> <a href="#Model-hasChanged">hasChanged</a></li>
  337. <li> <a href="#Model-changedAttributes">changedAttributes</a></li>
  338. <li> <a href="#Model-previous">previous</a></li>
  339. <li> <a href="#Model-previousAttributes">previousAttributes</a></li>
  340. </ul>
  341. <a class="toc_title" href="#Collection">
  342. Collection
  343. </a>
  344. <ul class="toc_section">
  345. <li> <a href="#Collection-extend">extend</a></li>
  346. <li> <a href="#Collection-model">model</a></li>
  347. <li> <a href="#Collection-constructor">constructor / initialize</a></li>
  348. <li> <a href="#Collection-models">models</a></li>
  349. <li> <a href="#Collection-toJSON">toJSON</a></li>
  350. <li> <a href="#Collection-sync">sync</a></li>
  351. <li> <a href="#Collection-Underscore-Methods"><b>Underscore Methods (28)</b></a></li>
  352. <li> <a href="#Collection-add">add</a></li>
  353. <li> <a href="#Collection-remove">remove</a></li>
  354. <li> <a href="#Collection-reset">reset</a></li>
  355. <li> <a href="#Collection-update">update</a></li>
  356. <li> <a href="#Collection-get">get</a></li>
  357. <li> <a href="#Collection-at">at</a></li>
  358. <li> <a href="#Collection-push">push</a></li>
  359. <li> <a href="#Collection-pop">pop</a></li>
  360. <li> <a href="#Collection-unshift">unshift</a></li>
  361. <li> <a href="#Collection-shift">shift</a></li>
  362. <li> <a href="#Collection-length">length</a></li>
  363. <li> <a href="#Collection-comparator">comparator</a></li>
  364. <li> <a href="#Collection-sort">sort</a></li>
  365. <li> <a href="#Collection-pluck">pluck</a></li>
  366. <li> <a href="#Collection-where">where</a></li>
  367. <li> <a href="#Collection-url">url</a></li>
  368. <li> <a href="#Collection-parse">parse</a></li>
  369. <li> <a href="#Collection-clone">clone</a></li>
  370. <li> <a href="#Collection-fetch">fetch</a></li>
  371. <li> <a href="#Collection-create">create</a></li>
  372. </ul>
  373. <a class="toc_title" href="#Router">
  374. Router
  375. </a>
  376. <ul class="toc_section">
  377. <li> <a href="#Router-extend">extend</a></li>
  378. <li> <a href="#Router-routes">routes</a></li>
  379. <li> <a href="#Router-constructor">constructor / initialize</a></li>
  380. <li> <a href="#Router-route">route</a></li>
  381. <li> <a href="#Router-navigate">navigate</a></li>
  382. </ul>
  383. <a class="toc_title" href="#History">
  384. History
  385. </a>
  386. <ul class="toc_section">
  387. <li> <a href="#History-start">start</a></li>
  388. </ul>
  389. <a class="toc_title" href="#Sync">
  390. Sync
  391. </a>
  392. <ul class="toc_section">
  393. <li> <a href="#Sync">Backbone.sync</a></li>
  394. <li> <a href="#Sync-ajax">Backbone.ajax</a></li>
  395. <li> <a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
  396. <li> <a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
  397. </ul>
  398. <a class="toc_title" href="#View">
  399. View
  400. </a>
  401. <ul class="toc_section">
  402. <li> <a href="#View-extend">extend</a></li>
  403. <li> <a href="#View-constructor">constructor / initialize</a></li>
  404. <li> <a href="#View-el">el</a></li>
  405. <li> <a href="#View-$el">$el</a></li>
  406. <li> <a href="#View-setElement">setElement</a></li>
  407. <li> <a href="#View-attributes">attributes</a></li>
  408. <li> <a href="#View-dollar">$ (jQuery or Zepto)</a></li>
  409. <li> <a href="#View-render">render</a></li>
  410. <li> <a href="#View-remove">remove</a></li>
  411. <li> <a href="#View-delegateEvents">delegateEvents</a></li>
  412. <li> <a href="#View-undelegateEvents">undelegateEvents</a></li>
  413. </ul>
  414. <a class="toc_title" href="#Utility">
  415. Utility
  416. </a>
  417. <ul class="toc_section">
  418. <li> <a href="#Utility-Backbone-noConflict">Backbone.noConflict</a></li>
  419. <li> <a href="#Utility-Backbone-$">Backbone.$</a></li>
  420. </ul>
  421. <a class="toc_title" href="#examples">
  422. Examples
  423. </a>
  424. <ul class="toc_section">
  425. <li> <a href="#examples-todos">Todos</a></li>
  426. <li> <a href="#examples-documentcloud">DocumentCloud</a></li>
  427. <li> <a href="#examples-usa-today">USA Today</a></li>
  428. <li> <a href="#examples-rdio">Rdio</a></li>
  429. <li> <a href="#examples-linkedin">LinkedIn Mobile</a></li>
  430. <li> <a href="#examples-hulu">Hulu</a></li>
  431. <li> <a href="#examples-flow">Flow</a></li>
  432. <li> <a href="#examples-gilt">Gilt Groupe</a></li>
  433. <li> <a href="#examples-newsblur">NewsBlur</a></li>
  434. <li> <a href="#examples-wordpress">WordPress.com</a></li>
  435. <li> <a href="#examples-foursquare">Foursquare</a></li>
  436. <li> <a href="#examples-bitbucket">Bitbucket</a></li>
  437. <li> <a href="#examples-disqus">Disqus</a></li>
  438. <li> <a href="#examples-khan-academy">Khan Academy</a></li>
  439. <li> <a href="#examples-do">Do</a></li>
  440. <li> <a href="#examples-irccloud">IRCCloud</a></li>
  441. <li> <a href="#examples-pitchfork">Pitchfork</a></li>
  442. <li> <a href="#examples-spin">Spin</a></li>
  443. <li> <a href="#examples-walmart">Walmart Mobile</a></li>
  444. <li> <a href="#examples-groupon">Groupon Now!</a></li>
  445. <li> <a href="#examples-basecamp">Basecamp</a></li>
  446. <li> <a href="#examples-slavery-footprint">Slavery Footprint</a></li>
  447. <li> <a href="#examples-stripe">Stripe</a></li>
  448. <li> <a href="#examples-airbnb">Airbnb</a></li>
  449. <li> <a href="#examples-diaspora">Diaspora</a></li>
  450. <li> <a href="#examples-soundcloud">SoundCloud Mobile</a></li>
  451. <li>- <a href="#examples-artsy">Art.sy</a></li>
  452. <li> <a href="#examples-pandora">Pandora</a></li>
  453. <li> <a href="#examples-inkling">Inkling</a></li>
  454. <li> <a href="#examples-code-school">Code School</a></li>
  455. <li> <a href="#examples-cloudapp">CloudApp</a></li>
  456. <li> <a href="#examples-seatgeek">SeatGeek</a></li>
  457. <li> <a href="#examples-easel">Easel</a></li>
  458. <li> <a href="#examples-prose">Prose</a></li>
  459. <li>- <a href="#examples-jolicloud">Jolicloud</a></li>
  460. <li> <a href="#examples-battlefield">Battlefield Play4Free</a></li>
  461. <li> <a href="#examples-syllabus">Syllabus</a></li>
  462. <li> <a href="#examples-salon">Salon.io</a></li>
  463. <li> <a href="#examples-tilemill">TileMill</a></li>
  464. <li> <a href="#examples-blossom">Blossom</a></li>
  465. <li> <a href="#examples-decide">Decide</a></li>
  466. <li> <a href="#examples-trello">Trello</a></li>
  467. <li> <a href="#examples-tzigla">Tzigla</a></li>
  468. </ul>
  469. <a class="toc_title" href="#faq">
  470. F.A.Q.
  471. </a>
  472. <ul class="toc_section">
  473. <li> <a href="#FAQ-why-backbone">Why Backbone?</a></li>
  474. <li> <a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
  475. <li> <a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
  476. <li> <a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
  477. <li> <a href="#FAQ-extending">Extending Backbone</a></li>
  478. <li> <a href="#FAQ-mvc">Traditional MVC</a></li>
  479. <li> <a href="#FAQ-this">Binding "this"</a></li>
  480. <li> <a href="#FAQ-rails">Working with Rails</a></li>
  481. </ul>
  482. <a class="toc_title" href="#changelog">
  483. Change Log
  484. </a>
  485. </div>
  486. <div class="container">
  487. <p>
  488. <img id="logo" src="docs/images/backbone.png" alt="Backbone.js" />
  489. </p>
  490. <p>
  491. Backbone.js gives structure to web applications
  492. by providing <b>models</b> with key-value binding and custom events,
  493. <b>collections</b> with a rich API of enumerable functions,
  494. <b>views</b> with declarative event handling, and connects it all to your
  495. existing API over a RESTful JSON interface.
  496. </p>
  497. <p>
  498. The project is <a href="http://github.com/documentcloud/backbone/">hosted on GitHub</a>,
  499. and the <a href="docs/backbone.html">annotated source code</a> is available,
  500. as well as an online <a href="test/">test suite</a>,
  501. an <a href="examples/todos/index.html">example application</a>,
  502. a <a href="https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>
  503. and a <a href="#examples">long list of real-world projects</a> that use Backbone.
  504. Backbone is available for use under the <a href="http://github.com/documentcloud/backbone/blob/master/LICENSE">MIT software license</a>.
  505. </p>
  506. <p>
  507. You can report bugs and discuss features on the
  508. <a href="http://github.com/documentcloud/backbone/issues">GitHub issues page</a>,
  509. on Freenode IRC in the <tt>#documentcloud</tt> channel, post questions to the
  510. <a href="https://groups.google.com/forum/#!forum/backbonejs">Google Group</a>,
  511. add pages to the <a href="https://github.com/documentcloud/backbone/wiki">wiki</a>
  512. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  513. </p>
  514. <p>
  515. <i>
  516. Backbone is an open-source component of
  517. <a href="http://documentcloud.org/">DocumentCloud</a>.
  518. </i>
  519. </p>
  520. <h2 id="downloads">
  521. Downloads &amp; Dependencies
  522. <span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
  523. </h2>
  524. <table>
  525. <tr>
  526. <td><a class="punch" href="backbone.js">Development Version (0.9.9)</a></td>
  527. <td class="text"><i>56kb, Full source, lots of comments</i></td>
  528. </tr>
  529. <tr>
  530. <td><a class="punch" href="backbone-min.js">Production Version (0.9.9)</a></td>
  531. <td class="text"><i>6.3kb, Packed and gzipped</i></td>
  532. </tr>
  533. <tr>
  534. <td><a class="punch" href="https://raw.github.com/documentcloud/backbone/master/backbone.js">Edge Version (master)</a></td>
  535. <td>
  536. <i>Unreleased, use at your own risk</i>
  537. <a href="https://travis-ci.org/documentcloud/backbone">
  538. <img width="89" height="13" src="https://travis-ci.org/documentcloud/backbone.png" />
  539. </a>
  540. </td>
  541. </tr>
  542. </table>
  543. <p>
  544. Backbone's only hard dependency is either
  545. <a href="http://underscorejs.org/">Underscore.js</a> <small>( >= 1.4.3)</small> or
  546. <a href="http://lodash.com">Lo-Dash</a>.
  547. For RESTful persistence, history support via <a href="#Router">Backbone.Router</a>
  548. and DOM manipulation with <a href="#View">Backbone.View</a>, include
  549. <a href="https://github.com/douglascrockford/JSON-js">json2.js</a>, and either
  550. <a href="http://jquery.com">jQuery</a> <small>( >= 1.7.0)</small> or
  551. <a href="http://zeptojs.com/">Zepto</a>.
  552. </p>
  553. <h2 id="introduction">Introduction</h2>
  554. <p>
  555. When working on a web application that involves a lot of JavaScript, one
  556. of the first things you learn is to stop tying your data to the DOM. It's all
  557. too easy to create JavaScript applications that end up as tangled piles of
  558. jQuery selectors and callbacks, all trying frantically to keep data in
  559. sync between the HTML UI, your JavaScript logic, and the database on your
  560. server. For rich client-side applications, a more structured approach
  561. is often helpful.
  562. </p>
  563. <p>
  564. With Backbone, you represent your data as
  565. <a href="#Model">Models</a>, which can be created, validated, destroyed,
  566. and saved to the server. Whenever a UI action causes an attribute of
  567. a model to change, the model triggers a <i>"change"</i> event; all
  568. the <a href="#View">Views</a> that display the model's state can be notified of the
  569. change, so that they are able to respond accordingly, re-rendering themselves with
  570. the new information. In a finished Backbone app, you don't have to write the glue
  571. code that looks into the DOM to find an element with a specific <i>id</i>,
  572. and update the HTML manually
  573. &mdash; when the model changes, the views simply update themselves.
  574. </p>
  575. <p>
  576. If you're new here, and aren't yet quite sure what Backbone is for, start by
  577. browsing the <a href="#examples">list of Backbone-based projects</a>.
  578. </p>
  579. <p>
  580. Many of the examples that follow are runnable. Click the <i>play</i> button
  581. to execute them.
  582. </p>
  583. <h2 id="upgrading">Upgrading to Edge</h2>
  584. <p>
  585. You're reading the docs for an unreleased version. See the
  586. <a href="#changelog">change log</a>.
  587. </p>
  588. <h2>Upgrading to 0.9.9</h2>
  589. <p>
  590. Backbone <b>0.9.9</b> should be considered as a release candidate
  591. for an upcoming <b>1.0</b>. If you're upgrading from a previous version,
  592. be sure to check the
  593. <a href="#changelog">change log</a>. In brief, a few of the larger changes
  594. are:
  595. </p>
  596. <ul>
  597. <li>
  598. Most importantly, Backbone events have two new methods:
  599. <a href="#Events-listenTo">listenTo</a> and
  600. <a href="#Events-stopListening">stopListening</a>. These are an
  601. inversion-of-control flavor of the usual <tt>on</tt> and <tt>off</tt>,
  602. and make it a little easier to clean up <i>all</i> events
  603. that an object is listening to on other objects. When you destroy Views
  604. with <a href="#View-remove">view.remove()</a>, this will now be done
  605. automatically. Note that the usual rules about programming in a garbage
  606. collected language still apply.
  607. </li>
  608. <li>
  609. HTTP <tt>PATCH</tt> support, via <tt>model.save(attrs, {patch: true})</tt>,
  610. if you'd like to send only partial updates to the server.
  611. </li>
  612. <li>
  613. An <a href="#Collection-update">update</a> method was added to Collection,
  614. which should make it easier to perform "smart" syncing updates with the
  615. server, where models are added or removed, and updated attributes are merged
  616. as appropriate. If you were previously using <br />
  617. <tt>collection.fetch({add: true})</tt>,
  618. try <a href="#Collection-update">update</a> instead.
  619. </li>
  620. <li>
  621. Backbone events now support <a href="#Events-once"</a>once</a>.
  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>
  671. All Backbone event methods also support an event map syntax, as an alternative
  672. to positional arguments:
  673. </p>
  674. <pre>
  675. book.on({
  676. "change:title": titleView.update,
  677. "change:author": authorPane.update,
  678. "destroy": bookView.remove
  679. });
  680. </pre>
  681. <p id="Events-off">
  682. <b class="header">off</b><code>object.off([event], [callback], [context])</code><span class="alias">Alias: unbind</span>
  683. <br />
  684. Remove a previously-bound <b>callback</b> function from an object. If no
  685. <b>context</b> is specified, all of the versions of the callback with
  686. different contexts will be removed. If no
  687. callback is specified, all callbacks for the <b>event</b> will be
  688. removed. If no event is specified, callbacks for <i>all</i> events
  689. will be removed.
  690. </p>
  691. <pre>
  692. // Removes just the `onChange` callback.
  693. object.off("change", onChange);
  694. // Removes all "change" callbacks.
  695. object.off("change");
  696. // Removes the `onChange` callback for all events.
  697. object.off(null, onChange);
  698. // Removes all callbacks for `context` for all events.
  699. object.off(null, null, context);
  700. // Removes all callbacks on `object`.
  701. object.off();
  702. </pre>
  703. <p>
  704. Note that calling <tt>model.off()</tt>, for example, will indeed remove <i>all</i> events
  705. on the model &mdash; including events that Backbone uses for internal bookkeeping.
  706. </p>
  707. <p id="Events-trigger">
  708. <b class="header">trigger</b><code>object.trigger(event, [*args])</code>
  709. <br />
  710. Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
  711. Subsequent arguments to <b>trigger</b> will be passed along to the
  712. event callbacks.
  713. </p>
  714. <p id="Events-once">
  715. <b class="header">once</b><code>object.once(event, callback, [context])</code>
  716. <br />
  717. Just like <a href="#Events-on">on</a>, but causes the bound callback to only
  718. fire once before being removed. Handy for saying "the next time that X happens, do this".
  719. </p>
  720. <p id="Events-listenTo">
  721. <b class="header">listenTo</b><code>object.listenTo(other, event, callback)</code>
  722. <br />
  723. Tell an <b>object</b> to listen to a particular event on an <b>other</b> object.
  724. The advantage of using this form, instead of <tt>other.on(event, callback)</tt>,
  725. is that <b>listenTo</b> allows the <b>object</b> to keep track of the events,
  726. and they can be removed all at once later on.
  727. </p>
  728. <pre>
  729. view.listenTo(model, 'change', view.render);
  730. </pre>
  731. <p id="Events-stopListening">
  732. <b class="header">stopListening</b><code>object.stopListening([other], [event], [callback])</code>
  733. <br />
  734. Tell an <b>object</b> to stop listening to events. Either call
  735. <b>stopListening</b> with no arguments to have the <b>object</b> remove
  736. all of its <a href="#Events-listenTo">registered</a> callbacks ... or be more
  737. precise by telling it to remove just the events it's listening to on a
  738. specific object, or a specific event, or just a specific callback.
  739. </p>
  740. <pre>
  741. view.stopListening();
  742. view.stopListening(model);
  743. </pre>
  744. <p id="Events-catalog">
  745. <b class="header">Catalog of Events</b>
  746. <br />
  747. Here's the complete list of built-in Backbone events, with arguments.
  748. You're also free to trigger your own events on Models, Collections and
  749. Views as you see fit. The <tt>Backbone</tt> object itself mixes in <tt>Events</tt>,
  750. and can be used to emit any global events that your application needs.
  751. </p>
  752. <ul class="small">
  753. <li><b>"add"</b> (model, collection, options) &mdash; when a model is added to a collection. </li>
  754. <li><b>"remove"</b> (model, collection, options) &mdash; when a model is removed from a collection. </li>
  755. <li><b>"reset"</b> (collection, options) &mdash; when the collection's entire contents have been replaced. </li>
  756. <li><b>"sort"</b> (collection, options) &mdash; when the collection has been re-sorted. </li>
  757. <li><b>"change"</b> (model, options) &mdash; when a model's attributes have changed. </li>
  758. <li><b>"change:[attribute]"</b> (model, value, options) &mdash; when a specific attribute has been updated. </li>
  759. <li><b>"destroy"</b> (model, collection, options) &mdash; when a model is <a href="#Model-destroy">destroyed</a>. </li>
  760. <li><b>"request"</b> (model, xhr, options) &mdash; when a model (or collection) has started a request to the server. </li>
  761. <li><b>"sync"</b> (model, resp, options) &mdash; when a model has been successfully synced with the server. </li>
  762. <li><b>"error"</b> (model, xhr, options) &mdash; when a model's <a href="#Model-save">save</a> call fails on the server. </li>
  763. <li><b>"invalid"</b> (model, error, options) &mdash; when a model's <a href="#Model-validate">validation</a> fails on the client. </li>
  764. <li><b>"route:[name]"</b> (params) &mdash; Fired by the router when a specific route is matched.</li>
  765. <li><b>"route"</b> (router, route, params) &mdash; Fired by history (or router) when <i>any</i> route has been matched.</li>
  766. <li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument. </li>
  767. </ul>
  768. <h2 id="Model">Backbone.Model</h2>
  769. <p>
  770. <b>Models</b> are the heart of any JavaScript application, containing
  771. the interactive data as well as a large part of the logic surrounding it:
  772. conversions, validations, computed properties, and access control. You
  773. extend <b>Backbone.Model</b> with your domain-specific methods, and
  774. <b>Model</b> provides a basic set of functionality for managing changes.
  775. </p>
  776. <p>
  777. The following is a contrived example, but it demonstrates defining a model
  778. with a custom method, setting an attribute, and firing an event keyed
  779. to changes in that specific attribute.
  780. After running this code once, <tt>sidebar</tt> will be
  781. available in your browser's console, so you can play around with it.
  782. </p>
  783. <pre class="runnable">
  784. var Sidebar = Backbone.Model.extend({
  785. promptColor: function() {
  786. var cssColor = prompt("Please enter a CSS color:");
  787. this.set({color: cssColor});
  788. }
  789. });
  790. window.sidebar = new Sidebar;
  791. sidebar.on('change:color', function(model, color) {
  792. $('#sidebar').css({background: color});
  793. });
  794. sidebar.set({color: 'white'});
  795. sidebar.promptColor();
  796. </pre>
  797. <p id="Model-extend">
  798. <b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
  799. <br />
  800. To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
  801. and provide instance <b>properties</b>, as well as optional
  802. <b>classProperties</b> to be attached directly to the constructor function.
  803. </p>
  804. <p>
  805. <b>extend</b> correctly sets up the prototype chain, so subclasses created
  806. with <b>extend</b> can be further extended and subclassed as far as you like.
  807. </p>
  808. <pre>
  809. var Note = Backbone.Model.extend({
  810. initialize: function() { ... },
  811. author: function() { ... },
  812. coordinates: function() { ... },
  813. allowedToEdit: function(account) {
  814. return true;
  815. }
  816. });
  817. var PrivateNote = Note.extend({
  818. allowedToEdit: function(account) {
  819. return account.owns(this);
  820. }
  821. });
  822. </pre>
  823. <p class="warning">
  824. Brief aside on <tt>super</tt>: JavaScript does not provide
  825. a simple way to call super &mdash; the function of the same name defined
  826. higher on the prototype chain. If you override a core function like
  827. <tt>set</tt>, or <tt>save</tt>, and you want to invoke the
  828. parent object's implementation, you'll have to explicitly call it, along these lines:
  829. </p>
  830. <pre>
  831. var Note = Backbone.Model.extend({
  832. set: function(attributes, options) {
  833. Backbone.Model.prototype.set.apply(this, arguments);
  834. ...
  835. }
  836. });
  837. </pre>
  838. <p id="Model-constructor">
  839. <b class="header">constructor / initialize</b><code>new Model([attributes], [options])</code>
  840. <br />
  841. When creating an instance of a model, you can pass in the initial values
  842. of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
  843. model. If you define an <b>initialize</b> function, it will be invoked when
  844. the model is created.
  845. </p>
  846. <pre>
  847. new Book({
  848. title: "One Thousand and One Nights",
  849. author: "Scheherazade"
  850. });
  851. </pre>
  852. <p>
  853. In rare cases, if you're looking to get fancy,
  854. you may want to override <b>constructor</b>, which allows
  855. you to replace the actual constructor function for your model.
  856. </p>
  857. <p>
  858. If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model
  859. gains a <tt>collection</tt> property that will be used to indicate which
  860. collection the model belongs to, and is used to help compute the model's
  861. <a href="#Model-url">url</a>. The <tt>model.collection</tt> property is
  862. otherwise added automatically when you first add a model to a collection.
  863. </p>
  864. <p id="Model-get">
  865. <b class="header">get</b><code>model.get(attribute)</code>
  866. <br />
  867. Get the current value of an attribute from the model. For example:
  868. <tt>note.get("title")</tt>
  869. </p>
  870. <p id="Model-set">
  871. <b class="header">set</b><code>model.set(attributes, [options])</code>
  872. <br />
  873. Set a hash of attributes (one or many) on the model. If any of the attributes
  874. change the model's state, a <tt>"change"</tt> event will be triggered, unless
  875. <tt>{silent: true}</tt> is passed as an option. Change events for specific
  876. attributes are also triggered, and you can bind to those as well, for example:
  877. <tt>change:title</tt>, and <tt>change:content</tt>. You may also pass
  878. individual keys and values.
  879. </p>
  880. <pre>
  881. note.set({title: "March 20", content: "In his eyes she eclipses..."});
  882. book.set("title", "A Scandal in Bohemia");
  883. </pre>
  884. <p>
  885. If the model has a <a href="#Model-validate">validate</a> method,
  886. it will be validated before the attributes are set, no changes will
  887. occur if the validation fails, and <b>set</b> will return <tt>false</tt>.
  888. Otherwise, <b>set</b> returns a reference to the model.
  889. You may also pass an <tt>error</tt>
  890. callback in the options, which will be invoked alongside the
  891. <tt>"error"</tt> event, should validation fail.
  892. </p>
  893. <p id="Model-escape">
  894. <b class="header">escape</b><code>model.escape(attribute)</code>
  895. <br />
  896. Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
  897. of a model's attribute. If you're interpolating data from the model into
  898. HTML, using <b>escape</b> to retrieve attributes will prevent
  899. <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
  900. </p>
  901. <pre class="runnable">
  902. var hacker = new Backbone.Model({
  903. name: "&lt;script&gt;alert('xss')&lt;/script&gt;"
  904. });
  905. alert(hacker.escape('name'));
  906. </pre>
  907. <p id="Model-has">
  908. <b class="header">has</b><code>model.has(attribute)</code>
  909. <br />
  910. Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
  911. value.
  912. </p>
  913. <pre>
  914. if (note.has("title")) {
  915. ...
  916. }
  917. </pre>
  918. <p id="Model-unset">
  919. <b class="header">unset</b><code>model.unset(attribute, [options])</code>
  920. <br />
  921. Remove an attribute by deleting it from the internal attributes hash.
  922. Fires a <tt>"change"</tt> event unless <tt>silent</tt> is passed as an option.
  923. </p>
  924. <p id="Model-clear">
  925. <b class="header">clear</b><code>model.clear([options])</code>
  926. <br />
  927. Removes all attributes from the model, including the <tt>id</tt> attribute. Fires a <tt>"change"</tt> event unless
  928. <tt>silent</tt> is passed as an option.
  929. </p>
  930. <p id="Model-id">
  931. <b class="header">id</b><code>model.id</code>
  932. <br />
  933. A special property of models, the <b>id</b> is an arbitrary string
  934. (integer id or UUID). If you set the <b>id</b> in the
  935. attributes hash, it will be copied onto the model as a direct property.
  936. Models can be retrieved by id from collections, and the id is used to generate
  937. model URLs by default.
  938. </p>
  939. <p id="Model-idAttribute">
  940. <b class="header">idAttribute</b><code>model.idAttribute</code>
  941. <br />
  942. A model's unique identifier is stored under the <tt>id</tt> attribute.
  943. If you're directly communicating with a backend (CouchDB, MongoDB) that uses
  944. a different unique key, you may set a Model's <tt>idAttribute</tt> to
  945. transparently map from that key to <tt>id</tt>.
  946. <pre class="runnable">
  947. var Meal = Backbone.Model.extend({
  948. idAttribute: "_id"
  949. });
  950. var cake = new Meal({ _id: 1, name: "Cake" });
  951. alert("Cake id: " + cake.id);
  952. </pre>
  953. </p>
  954. <p id="Model-cid">
  955. <b class="header">cid</b><code>model.cid</code>
  956. <br />
  957. A special property of models, the <b>cid</b> or client id is a unique identifier
  958. automatically assigned to all models when they're first created. Client ids
  959. are handy when the model has not yet been saved to the server, and does not
  960. yet have its eventual true <b>id</b>, but already needs to be visible in the UI.
  961. </p>
  962. <p id="Model-attributes">
  963. <b class="header">attributes</b><code>model.attributes</code>
  964. <br />
  965. The <b>attributes</b> property is the internal hash containing the model's
  966. state &mdash; usually (but not necessarily) a form of the JSON object
  967. representing the model data on the server. It's often a straightforward
  968. serialization of a row from the database, but it could also be client-side
  969. computed state.
  970. </p>
  971. <p>
  972. Please use <a href="#Model-set">set</a> to update the <b>attributes</b>
  973. instead of modifying them directly. If you'd like to retrieve and munge a
  974. copy of the model's attributes, use <a href="#Model-toJSON">toJSON</a>
  975. instead.
  976. </p>
  977. <p class="warning">
  978. Due to the fact that <a href="#Events">Events</a> accepts space separated
  979. lists of events, attribute names should not include spaces.
  980. </p>
  981. <p id="Model-changed">
  982. <b class="header">changed</b><code>model.changed</code>
  983. <br />
  984. The <b>changed</b> property is the internal hash containing all the attributes
  985. that have changed since the last <tt>"change"</tt> event was triggered.
  986. Please do not update <b>changed</b> directly. Its state is maintained internally
  987. by <a href="#Model-set">set</a> and <a href="#Model-change">change</a>.
  988. A copy of <b>changed</b> can be acquired from
  989. <a href="#Model-changedAttributes">changedAttributes</a>.
  990. </p>
  991. <p id="Model-defaults">
  992. <b class="header">defaults</b><code>model.defaults or model.defaults()</code>
  993. <br />
  994. The <b>defaults</b> hash (or function) can be used to specify the default
  995. attributes for your model. When creating an instance of the model,
  996. any unspecified attributes will be set to their default value.
  997. </p>
  998. <pre class="runnable">
  999. var Meal = Backbone.Model.extend({
  1000. defaults: {
  1001. "appetizer": "caesar salad",
  1002. "entree": "ravioli",
  1003. "dessert": "cheesecake"
  1004. }
  1005. });
  1006. alert("Dessert will be " + (new Meal).get('dessert'));
  1007. </pre>
  1008. <p class="warning">
  1009. Remember that in JavaScript, objects are passed by reference, so if you
  1010. include an object as a default value, it will be shared among all instances.
  1011. Instead, define <b>defaults</b> as a function.
  1012. </p>
  1013. <p id="Model-toJSON">
  1014. <b class="header">toJSON</b><code>model.toJSON()</code>
  1015. <br />
  1016. Return a copy of the model's <a href="#Model-attributes">attributes</a> for JSON stringification.
  1017. This can be used for persistence, serialization, or for augmentation before
  1018. being handed off to a view. The name of this method is a bit confusing, as
  1019. it doesn't actually return a JSON string &mdash; but I'm afraid that it's
  1020. the way that the <a href="https://developer.mozilla.org/en/JSON#toJSON()_method">JavaScript API for <b>JSON.stringify</b> works</a>.
  1021. </p>
  1022. <pre class="runnable">
  1023. var artist = new Backbone.Model({
  1024. firstName: "Wassily",
  1025. lastName: "Kandinsky"
  1026. });
  1027. artist.set({birthday: "December 16, 1866"});
  1028. alert(JSON.stringify(artist));
  1029. </pre>
  1030. <p id="Model-sync">
  1031. <b class="header">sync</b><code>model.sync(method, model, [options])</code>
  1032. <br />
  1033. Uses <a href="#Sync">Backbone.sync</a> to persist the state of a model to
  1034. the server. Can be overridden for custom behavior.
  1035. </p>
  1036. <p id="Model-fetch">
  1037. <b class="header">fetch</b><code>model.fetch([options])</code>
  1038. <br />
  1039. Resets the model's state from the server by delegating to
  1040. <a href="#Sync">Backbone.sync</a>. Returns a
  1041. <a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a>.
  1042. Useful if the model has never
  1043. been populated with data, or if you'd like to ensure that you have the
  1044. latest server state. A <tt>"change"</tt> event will be triggered if the
  1045. server's state differs from the current attributes. Accepts
  1046. <tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
  1047. are passed <tt>(model, response, options)</tt>
  1048. and <tt>(model, xhr, options)</tt> as arguments, respectively.
  1049. </p>
  1050. <pre>
  1051. // Poll every 10 seconds to keep the channel model up-to-date.
  1052. setInterval(function() {
  1053. channel.fetch();
  1054. }, 10000);
  1055. </pre>
  1056. <p id="Model-save">
  1057. <b class="header">save</b><code>model.save([attributes], [options])</code>
  1058. <br />
  1059. Save a model to your database (or alternative persistence layer),
  1060. by delegating to <a href="#Sync">Backbone.sync</a>. Returns a
  1061. <a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> if
  1062. validation is successful and <tt>false</tt> otherwise. The <b>attributes</b>
  1063. hash (as in <a href="#Model-set">set</a>) should contain the attributes
  1064. you'd like to change &mdash; keys that aren't mentioned won't be altered &mdash; but,
  1065. a <i>complete representation</i> of the resource will be sent to the server.
  1066. As with <tt>set</tt>, you may pass individual keys and values instead of a hash.
  1067. If the model has a <a href="#Model-validate">validate</a>
  1068. method, and validation fails, the model will not be saved. If the model
  1069. <a href="#Model-isNew">isNew</a>, the save will be a <tt>"create"</tt>
  1070. (HTTP <tt>POST</tt>), if the model already
  1071. exists on the server, the save will be an <tt>"update"</tt> (HTTP <tt>PUT</tt>).
  1072. </p>
  1073. <p>
  1074. If instead, you'd only like the <i>changed</i> attributes to be sent to the
  1075. server, call <tt>model.save(attrs, {patch: true})</tt>. You'll get an HTTP
  1076. <tt>PATCH</tt> request to the server with just the passed-in attributes.
  1077. </p>
  1078. <p>
  1079. Calling <tt>save</tt> with new attributes will cause a <tt>"change"</tt>
  1080. event immediately, a <tt>"request"</tt> event as the Ajax request begins to
  1081. go to the server, and a <tt>"sync"</tt> event after the server has acknowledged
  1082. the successful change. Pass <tt>{wait: true}</tt> if you'd like to wait
  1083. for the server before setting the new attributes on the model.
  1084. </p>
  1085. <p>
  1086. In the following example, notice how our overridden version
  1087. of <tt>Backbone.sync</tt> receives a <tt>"create"</tt> request
  1088. the first time the model is saved and an <tt>"update"</tt>
  1089. request the second time.
  1090. </p>
  1091. <pre class="runnable">
  1092. Backbone.sync = function(method, model) {
  1093. alert(method + ": " + JSON.stringify(model));
  1094. model.id = 1;
  1095. };
  1096. var book = new Backbone.Model({
  1097. title: "The Rough Riders",
  1098. author: "Theodore Roosevelt"
  1099. });
  1100. book.save();
  1101. book.save({author: "Teddy"});
  1102. </pre>
  1103. <p>
  1104. <b>save</b> accepts <tt>success</tt> and <tt>error</tt> callbacks in the
  1105. options hash, which are passed <tt>(model, response, options)</tt> and
  1106. <tt>(model, xhr, options)</tt> as arguments, respectively.
  1107. The <tt>error</tt> callback will also be invoked if the model has a
  1108. <tt>validate</tt> method, and validation fails. If a server-side
  1109. validation fails, return a non-<tt>200</tt> HTTP response code, along with
  1110. an error response in text or JSON.
  1111. </p>
  1112. <pre>
  1113. book.save("author", "F.D.R.", {error: function(){ ... }});
  1114. </pre>
  1115. <p id="Model-destroy">
  1116. <b class="header">destroy</b><code>model.destroy([options])</code>
  1117. <br />
  1118. Destroys the model on the server by delegating an HTTP <tt>DELETE</tt>
  1119. request to <a href="#Sync">Backbone.sync</a>. Returns a
  1120. <a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jqXHR</a> object, or
  1121. <tt>false</tt> if the model <a href="#Model-isNew">isNew</a>. Accepts
  1122. <tt>success</tt> and <tt>error</tt> callbacks in the options hash, which
  1123. are passed <tt>(model, response, options)</tt> and <tt>(model, xhr, options)</tt>
  1124. as arguments, respectively.
  1125. Triggers a <tt>"destroy"</tt> event on the model, which will bubble up
  1126. through any collections that contain it, a <tt>"request"</tt> event as it
  1127. begins the Ajax request to the server, and a <tt>"sync"</tt> event, after
  1128. the server has successfully acknowledged the model's deletion. Pass
  1129. <tt>{wait: true}</tt> if you'd like to wait for the server to respond
  1130. before removing the model from the collection.
  1131. </p>
  1132. <pre>
  1133. book.destroy({success: function(model, response) {
  1134. ...
  1135. }});
  1136. </pre>
  1137. <p id="Model-validate">
  1138. <b class="header">validate</b><code>model.validate(attributes, options)</code>
  1139. <br />
  1140. This method is left undefined, and you're encouraged to override it with
  1141. your custom validation logic, if you have any that can be performed
  1142. in JavaScript. By default <b>validate</b> is called before
  1143. <tt>save</tt>, but can also be called before <tt>set</tt> if
  1144. <tt>{validate:true}</tt> is passed. The <b>validate</b> method is passed
  1145. the model attributes, as well as the options from <tt>set</tt> or <tt>save</tt>.
  1146. If the attributes are valid, don't return anything from <b>validate</b>;
  1147. if they are invalid, return an error of your choosing. It
  1148. can be as simple as a string error message to be displayed, or a complete
  1149. error object that describes the error programmatically. If <b>validate</b>
  1150. returns an error, <tt>set</tt> and <tt>save</tt> will not continue, and the
  1151. model attributes will not be modified.
  1152. Failed validations trigger an <tt>"error"</tt> event.
  1153. </p>
  1154. <pre class="runnable">
  1155. var Chapter = Backbone.Model.extend({
  1156. validate: function(attrs, options) {
  1157. if (attrs.end &lt; attrs.start) {
  1158. return "can't end before it starts";
  1159. }
  1160. }
  1161. });
  1162. var one = new Chapter({
  1163. title : "Chapter One: The Beginning"
  1164. });
  1165. one.on("error", function(model, error) {
  1166. alert(model.get("title") + " " + error);
  1167. });
  1168. one.set({
  1169. start: 15,
  1170. end: 10
  1171. });
  1172. </pre>
  1173. <p>
  1174. <tt>"error"</tt> events are useful for providing coarse-grained error
  1175. messages at the model or collection level. An <tt>error</tt> callback can
  1176. can also be specified in the options

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