PageRenderTime 79ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

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

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