PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bower_components/backbone/index.html

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

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