PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/index.html

https://gitlab.com/JJVV27/backbone
HTML | 4878 lines | 4294 code | 584 blank | 0 comment | 0 complexity | 41a36c312559ec3f0f3e76d2dc9504fb 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. img.figure {
  282. width: 100%;
  283. }
  284. div.columns {
  285. display: table;
  286. table-layout: fixed;
  287. width: 100%;
  288. }
  289. div.columns ul {
  290. margin: 10px 0;
  291. }
  292. div.col-50 {
  293. display: table-cell;
  294. width: 50%;
  295. }
  296. </style>
  297. </head>
  298. <body>
  299. <div id="sidebar" class="interface">
  300. <a class="toc_title" href="#">
  301. Backbone.js <span class="version">(1.2.0)</span>
  302. </a>
  303. <ul class="toc_section">
  304. <li>&raquo; <a href="http://github.com/jashkenas/backbone">GitHub Repository</a></li>
  305. <li>&raquo; <a href="docs/backbone.html">Annotated Source</a></li>
  306. </ul>
  307. <a class="toc_title" href="#Getting-started">
  308. Getting Started
  309. </a>
  310. <ul class="toc_section">
  311. <li>- <a href="#Getting-Started">Introduction</a></li>
  312. <li> <a href="#Model-View-separation">Models and Views</a></li>
  313. <li> <a href="#Model-Collections">Collections</a></li>
  314. <li> <a href="#API-integration">API Integration</a></li>
  315. <li> <a href="#View-rendering">Rendering</a></li>
  316. <li> <a href="#Routing">Routing</a></li>
  317. </ul>
  318. <a class="toc_title" href="#Events">
  319. Events
  320. </a>
  321. <ul class="toc_section">
  322. <li> <a href="#Events-on">on</a></li>
  323. <li> <a href="#Events-off">off</a></li>
  324. <li> <a href="#Events-trigger">trigger</a></li>
  325. <li> <a href="#Events-once">once</a></li>
  326. <li> <a href="#Events-listenTo">listenTo</a></li>
  327. <li> <a href="#Events-stopListening">stopListening</a></li>
  328. <li> <a href="#Events-listenToOnce">listenToOnce</a></li>
  329. <li>- <a href="#Events-catalog"><b>Catalog of Built-in Events</b></a></li>
  330. </ul>
  331. <a class="toc_title" href="#Model">
  332. Model
  333. </a>
  334. <ul class="toc_section">
  335. <li> <a href="#Model-extend">extend</a></li>
  336. <li> <a href="#Model-constructor">constructor / initialize</a></li>
  337. <li> <a href="#Model-get">get</a></li>
  338. <li> <a href="#Model-set">set</a></li>
  339. <li> <a href="#Model-escape">escape</a></li>
  340. <li> <a href="#Model-has">has</a></li>
  341. <li> <a href="#Model-unset">unset</a></li>
  342. <li> <a href="#Model-clear">clear</a></li>
  343. <li> <a href="#Model-id">id</a></li>
  344. <li> <a href="#Model-idAttribute">idAttribute</a></li>
  345. <li> <a href="#Model-cid">cid</a></li>
  346. <li> <a href="#Model-attributes">attributes</a></li>
  347. <li> <a href="#Model-changed">changed</a></li>
  348. <li> <a href="#Model-defaults">defaults</a></li>
  349. <li> <a href="#Model-toJSON">toJSON</a></li>
  350. <li> <a href="#Model-sync">sync</a></li>
  351. <li> <a href="#Model-fetch">fetch</a></li>
  352. <li> <a href="#Model-save">save</a></li>
  353. <li> <a href="#Model-destroy">destroy</a></li>
  354. <li> <a href="#Model-Underscore-Methods"><b>Underscore Methods (9)</b></a></li>
  355. <li> <a href="#Model-validate">validate</a></li>
  356. <li> <a href="#Model-validationError">validationError</a></li>
  357. <li> <a href="#Model-isValid">isValid</a></li>
  358. <li> <a href="#Model-url">url</a></li>
  359. <li> <a href="#Model-urlRoot">urlRoot</a></li>
  360. <li> <a href="#Model-parse">parse</a></li>
  361. <li> <a href="#Model-clone">clone</a></li>
  362. <li> <a href="#Model-isNew">isNew</a></li>
  363. <li> <a href="#Model-hasChanged">hasChanged</a></li>
  364. <li> <a href="#Model-changedAttributes">changedAttributes</a></li>
  365. <li> <a href="#Model-previous">previous</a></li>
  366. <li> <a href="#Model-previousAttributes">previousAttributes</a></li>
  367. </ul>
  368. <a class="toc_title" href="#Collection">
  369. Collection
  370. </a>
  371. <ul class="toc_section">
  372. <li> <a href="#Collection-extend">extend</a></li>
  373. <li> <a href="#Collection-model">model</a></li>
  374. <li> <a href="#Collection-constructor">constructor / initialize</a></li>
  375. <li> <a href="#Collection-models">models</a></li>
  376. <li> <a href="#Collection-toJSON">toJSON</a></li>
  377. <li> <a href="#Collection-sync">sync</a></li>
  378. <li> <a href="#Collection-Underscore-Methods"><b>Underscore Methods (46)</b></a></li>
  379. <li> <a href="#Collection-add">add</a></li>
  380. <li> <a href="#Collection-remove">remove</a></li>
  381. <li> <a href="#Collection-reset">reset</a></li>
  382. <li> <a href="#Collection-set">set</a></li>
  383. <li> <a href="#Collection-get">get</a></li>
  384. <li> <a href="#Collection-at">at</a></li>
  385. <li> <a href="#Collection-push">push</a></li>
  386. <li> <a href="#Collection-pop">pop</a></li>
  387. <li> <a href="#Collection-unshift">unshift</a></li>
  388. <li> <a href="#Collection-shift">shift</a></li>
  389. <li> <a href="#Collection-slice">slice</a></li>
  390. <li> <a href="#Collection-length">length</a></li>
  391. <li> <a href="#Collection-comparator">comparator</a></li>
  392. <li> <a href="#Collection-sort">sort</a></li>
  393. <li> <a href="#Collection-pluck">pluck</a></li>
  394. <li> <a href="#Collection-where">where</a></li>
  395. <li> <a href="#Collection-findWhere">findWhere</a></li>
  396. <li> <a href="#Collection-url">url</a></li>
  397. <li> <a href="#Collection-parse">parse</a></li>
  398. <li> <a href="#Collection-clone">clone</a></li>
  399. <li> <a href="#Collection-fetch">fetch</a></li>
  400. <li> <a href="#Collection-create">create</a></li>
  401. </ul>
  402. <a class="toc_title" href="#Router">
  403. Router
  404. </a>
  405. <ul class="toc_section">
  406. <li> <a href="#Router-extend">extend</a></li>
  407. <li> <a href="#Router-routes">routes</a></li>
  408. <li> <a href="#Router-constructor">constructor / initialize</a></li>
  409. <li> <a href="#Router-route">route</a></li>
  410. <li> <a href="#Router-navigate">navigate</a></li>
  411. <li> <a href="#Router-execute">execute</a></li>
  412. </ul>
  413. <a class="toc_title" href="#History">
  414. History
  415. </a>
  416. <ul class="toc_section">
  417. <li> <a href="#History-start">start</a></li>
  418. </ul>
  419. <a class="toc_title" href="#Sync">
  420. Sync
  421. </a>
  422. <ul class="toc_section">
  423. <li> <a href="#Sync">Backbone.sync</a></li>
  424. <li> <a href="#Sync-ajax">Backbone.ajax</a></li>
  425. <li> <a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
  426. <li> <a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
  427. </ul>
  428. <a class="toc_title" href="#View">
  429. View
  430. </a>
  431. <ul class="toc_section">
  432. <li> <a href="#View-extend">extend</a></li>
  433. <li> <a href="#View-constructor">constructor / initialize</a></li>
  434. <li> <a href="#View-el">el</a></li>
  435. <li> <a href="#View-$el">$el</a></li>
  436. <li> <a href="#View-setElement">setElement</a></li>
  437. <li> <a href="#View-attributes">attributes</a></li>
  438. <li> <a href="#View-dollar">$ (jQuery)</a></li>
  439. <li> <a href="#View-template">template</a></li>
  440. <li> <a href="#View-render">render</a></li>
  441. <li> <a href="#View-remove">remove</a></li>
  442. <li> <a href="#View-delegateEvents">delegateEvents</a></li>
  443. <li> <a href="#View-undelegateEvents">undelegateEvents</a></li>
  444. </ul>
  445. <a class="toc_title" href="#Utility">
  446. Utility
  447. </a>
  448. <ul class="toc_section">
  449. <li> <a href="#Utility-Backbone-noConflict">Backbone.noConflict</a></li>
  450. <li> <a href="#Utility-Backbone-$">Backbone.$</a></li>
  451. </ul>
  452. <a class="toc_title" href="#faq">
  453. F.A.Q.
  454. </a>
  455. <ul class="toc_section">
  456. <li> <a href="#FAQ-why-backbone">Why Backbone?</a></li>
  457. <li> <a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
  458. <li> <a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
  459. <li> <a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
  460. <li> <a href="#FAQ-extending">Extending Backbone</a></li>
  461. <li> <a href="#FAQ-mvc">Traditional MVC</a></li>
  462. <li> <a href="#FAQ-this">Binding "this"</a></li>
  463. <li> <a href="#FAQ-rails">Working with Rails</a></li>
  464. </ul>
  465. <a class="toc_title" href="#examples">
  466. Examples
  467. </a>
  468. <ul class="toc_section">
  469. <li> <a href="#examples-todos">Todos</a></li>
  470. <li> <a href="#examples-documentcloud">DocumentCloud</a></li>
  471. <li> <a href="#examples-usa-today">USA Today</a></li>
  472. <li> <a href="#examples-rdio">Rdio</a></li>
  473. <li> <a href="#examples-hulu">Hulu</a></li>
  474. <li> <a href="#examples-quartz">Quartz</a></li>
  475. <li> <a href="#examples-earth">Earth</a></li>
  476. <li> <a href="#examples-vox">Vox</a></li>
  477. <li> <a href="#examples-gawker">Gawker Media</a></li>
  478. <li> <a href="#examples-flow">Flow</a></li>
  479. <li> <a href="#examples-gilt">Gilt Groupe</a></li>
  480. <li> <a href="#examples-enigma">Enigma</a></li>
  481. <li> <a href="#examples-newsblur">NewsBlur</a></li>
  482. <li> <a href="#examples-wordpress">WordPress.com</a></li>
  483. <li> <a href="#examples-foursquare">Foursquare</a></li>
  484. <li> <a href="#examples-bitbucket">Bitbucket</a></li>
  485. <li> <a href="#examples-disqus">Disqus</a></li>
  486. <li> <a href="#examples-delicious">Delicious</a></li>
  487. <li> <a href="#examples-khan-academy">Khan Academy</a></li>
  488. <li> <a href="#examples-irccloud">IRCCloud</a></li>
  489. <li> <a href="#examples-pitchfork">Pitchfork</a></li>
  490. <li> <a href="#examples-spin">Spin</a></li>
  491. <li> <a href="#examples-zocdoc">ZocDoc</a></li>
  492. <li> <a href="#examples-walmart">Walmart Mobile</a></li>
  493. <li> <a href="#examples-groupon">Groupon Now!</a></li>
  494. <li> <a href="#examples-basecamp">Basecamp</a></li>
  495. <li> <a href="#examples-slavery-footprint">Slavery Footprint</a></li>
  496. <li> <a href="#examples-stripe">Stripe</a></li>
  497. <li> <a href="#examples-airbnb">Airbnb</a></li>
  498. <li> <a href="#examples-soundcloud">SoundCloud Mobile</a></li>
  499. <li>- <a href="#examples-artsy">Art.sy</a></li>
  500. <li> <a href="#examples-pandora">Pandora</a></li>
  501. <li> <a href="#examples-inkling">Inkling</a></li>
  502. <li> <a href="#examples-code-school">Code School</a></li>
  503. <li> <a href="#examples-cloudapp">CloudApp</a></li>
  504. <li> <a href="#examples-seatgeek">SeatGeek</a></li>
  505. <li> <a href="#examples-easel">Easel</a></li>
  506. <li>- <a href="#examples-jolicloud">Jolicloud</a></li>
  507. <li> <a href="#examples-salon">Salon.io</a></li>
  508. <li> <a href="#examples-tilemill">TileMill</a></li>
  509. <li> <a href="#examples-blossom">Blossom</a></li>
  510. <li> <a href="#examples-trello">Trello</a></li>
  511. <li> <a href="#examples-tzigla">Tzigla</a></li>
  512. </ul>
  513. <a class="toc_title" href="#changelog">
  514. Change Log
  515. </a>
  516. </div>
  517. <div class="container">
  518. <p>
  519. <img id="logo" src="docs/images/backbone.png" alt="Backbone.js" />
  520. </p>
  521. <p>
  522. Backbone.js gives structure to web applications
  523. by providing <b>models</b> with key-value binding and custom events,
  524. <b>collections</b> with a rich API of enumerable functions,
  525. <b>views</b> with declarative event handling, and connects it all to your
  526. existing API over a RESTful JSON interface.
  527. </p>
  528. <p>
  529. The project is <a href="http://github.com/jashkenas/backbone/">hosted on GitHub</a>,
  530. and the <a href="docs/backbone.html">annotated source code</a> is available,
  531. as well as an online <a href="test/">test suite</a>,
  532. an <a href="examples/todos/index.html">example application</a>,
  533. a <a href="https://github.com/jashkenas/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>
  534. and a <a href="#examples">long list of real-world projects</a> that use Backbone.
  535. Backbone is available for use under the <a href="http://github.com/jashkenas/backbone/blob/master/LICENSE">MIT software license</a>.
  536. </p>
  537. <p>
  538. You can report bugs and discuss features on the
  539. <a href="http://github.com/jashkenas/backbone/issues">GitHub issues page</a>,
  540. on Freenode IRC in the <tt>#documentcloud</tt> channel, post questions to the
  541. <a href="https://groups.google.com/forum/#!forum/backbonejs">Google Group</a>,
  542. add pages to the <a href="https://github.com/jashkenas/backbone/wiki">wiki</a>
  543. or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
  544. </p>
  545. <p>
  546. <i>
  547. Backbone is an open-source component of
  548. <a href="http://documentcloud.org/">DocumentCloud</a>.
  549. </i>
  550. </p>
  551. <h2 id="downloads">
  552. Downloads &amp; Dependencies
  553. <span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
  554. </h2>
  555. <table>
  556. <tr>
  557. <td><a class="punch" href="backbone.js">Development Version (1.2.0)</a></td>
  558. <td class="text"><i>69kb, Full source, tons of comments</i></td>
  559. </tr>
  560. <tr>
  561. <td><a class="punch" href="backbone-min.js">Production Version (1.2.0)</a></td>
  562. <td class="text" style="line-height: 16px;">
  563. <i>7.3kb, Packed and gzipped</i><br />
  564. <small>(<a href="backbone-min.map">Source Map</a>)</small>
  565. </td>
  566. </tr>
  567. <tr>
  568. <td><a class="punch" href="https://raw.github.com/jashkenas/backbone/master/backbone.js">Edge Version (master)</a></td>
  569. <td>
  570. <i>Unreleased, use at your own risk</i>
  571. <a class="travis-badge" href="https://travis-ci.org/jashkenas/backbone">
  572. <img src="https://travis-ci.org/jashkenas/backbone.png" />
  573. </a>
  574. </td>
  575. </tr>
  576. </table>
  577. <p>
  578. Backbone's only hard dependency is
  579. <a href="http://underscorejs.org/">Underscore.js</a> <small>( >= 1.7.0)</small>.
  580. For RESTful persistence and DOM manipulation with <a href="#View">Backbone.View</a>,
  581. include <b><a href="http://jquery.com">jQuery</a></b> ( >= 1.11.0), and
  582. <b><a href="https://github.com/douglascrockford/JSON-js">json2.js</a></b> for older
  583. Internet Explorer support.
  584. <i>(Mimics of the Underscore and jQuery APIs, such as
  585. <a href="http://lodash.com">Lo-Dash</a> and
  586. <a href="http://zeptojs.com">Zepto</a>, will
  587. also tend to work, with varying degrees of compatibility.)</i>
  588. </p>
  589. <h2 id="Getting-started">Getting Started</h2>
  590. <p>
  591. When working on a web application that involves a lot of JavaScript, one
  592. of the first things you learn is to stop tying your data to the DOM. It's all
  593. too easy to create JavaScript applications that end up as tangled piles of
  594. jQuery selectors and callbacks, all trying frantically to keep data in
  595. sync between the HTML UI, your JavaScript logic, and the database on your
  596. server. For rich client-side applications, a more structured approach
  597. is often helpful.
  598. </p>
  599. <p>
  600. With Backbone, you represent your data as
  601. <a href="#Model">Models</a>, which can be created, validated, destroyed,
  602. and saved to the server. Whenever a UI action causes an attribute of
  603. a model to change, the model triggers a <i>"change"</i> event; all
  604. the <a href="#View">Views</a> that display the model's state can be notified of the
  605. change, so that they are able to respond accordingly, re-rendering themselves with
  606. the new information. In a finished Backbone app, you don't have to write the glue
  607. code that looks into the DOM to find an element with a specific <i>id</i>,
  608. and update the HTML manually
  609. &mdash; when the model changes, the views simply update themselves.
  610. </p>
  611. <p>
  612. Philosophically, Backbone is an attempt to discover the minimal set
  613. of data-structuring (models and collections) and user interface (views
  614. and URLs) primitives that are generally useful when building web applications with
  615. JavaScript. In an ecosystem where overarching, decides-everything-for-you
  616. frameworks are commonplace, and many libraries require your site to be
  617. reorganized to suit their look, feel, and default behavior Backbone should
  618. continue to be a tool that gives you the <i>freedom</i> to design the full
  619. experience of your web application.
  620. </p>
  621. <p>
  622. If you're new here, and aren't yet quite sure what Backbone is for, start by
  623. browsing the <a href="#examples">list of Backbone-based projects</a>.
  624. </p>
  625. <p>
  626. Many of the code examples in this documentation are runnable, because
  627. Backbone is included on this page.
  628. Click the <i>play</i> button to execute them.
  629. </p>
  630. <h2 id="Model-View-separation">Models and Views</h2>
  631. <img class="figure" src="docs/images/intro-model-view.svg" alt="Model-View Separation.">
  632. <p>
  633. The single most important thing that Backbone can help you with is keeping
  634. your business logic separate from your user interface. When the two are
  635. entangled, change is hard; when logic doesn't depend on UI, your
  636. interface becomes easier to work with.
  637. </p>
  638. <div class="columns">
  639. <div class="col-50">
  640. <b>Model</b>
  641. <ul>
  642. <li>Orchestrates data and business logic.</li>
  643. <li>Loads and saves from the server.</li>
  644. <li>Emits events when data changes.</li>
  645. </ul>
  646. </div>
  647. <div class="col-50">
  648. <b>View</b>
  649. <ul>
  650. <li>Listens for changes and renders UI.</li>
  651. <li>Handles user input and interactivity.</li>
  652. <li>Sends captured input to the model.</li>
  653. </ul>
  654. </div>
  655. </div>
  656. <p>
  657. A <b>Model</b> manages an internal table of data attributes, and
  658. triggers <tt>"change"</tt> events when any of its data is modified.
  659. Models handle syncing data with a persistence layer usually a REST API
  660. with a backing database. Design your models as the atomic reusable objects
  661. containing all of the helpful functions for manipulating their particular
  662. bit of data. Models should be able to be passed around throughout your app,
  663. and used anywhere that bit of data is needed.
  664. </p>
  665. <p>
  666. A <b>View</b> is an atomic chunk of user interface. It often renders the
  667. data from a specific model, or number of models &mdash; but views can
  668. also be data-less chunks of UI that stand alone.
  669. Models should be generally unaware of views. Instead, views listen to
  670. the model <tt>"change"</tt> events, and react or re-render themselves
  671. appropriately.
  672. </p>
  673. <h2 id="Model-Collections">Collections</h2>
  674. <img class="figure" src="docs/images/intro-collections.svg" alt="Model Collections.">
  675. <p>
  676. A <b>Collection</b> helps you deal with a group of related models, handling
  677. the loading and saving of new models to the server and providing helper
  678. functions for performing aggregations or computations against a list of models.
  679. Aside from their own events, collections also proxy through all of the
  680. events that occur to models within them, allowing you to listen in one place
  681. for any change that might happen to any model in the collection.
  682. </p>
  683. <h2 id="API-integration">API Integration</h2>
  684. <p>
  685. Backbone is pre-configured to sync with a RESTful API. Simply create a
  686. new Collection with the <tt>url</tt> of your resource endpoint:
  687. </p>
  688. <pre>
  689. var Books = Backbone.Collection.extend({
  690. url: '/books'
  691. });
  692. </pre>
  693. <p>
  694. The <b>Collection</b> and <b>Model</b> components together form a direct
  695. mapping of REST resources using the following methods:
  696. </p>
  697. <pre>
  698. GET /books/ .... collection.fetch();
  699. POST /books/ .... collection.create();
  700. GET /books/1 ... model.fetch();
  701. PUT /books/1 ... model.save();
  702. DEL /books/1 ... model.destroy();
  703. </pre>
  704. <p>
  705. When fetching raw JSON data from an API, a <b>Collection</b> will
  706. automatically populate itself with data formatted as an array, while
  707. a <b>Model</b> will automatically populate itself with data formatted
  708. as an object:
  709. </p>
  710. <pre>
  711. [{"id": 1}] ..... populates a Collection with one model.
  712. {"id": 1} ....... populates a Model with one attribute.
  713. </pre>
  714. <p>
  715. However, it's fairly common to encounter APIs that return data in a
  716. different format than what Backbone expects. For example, consider
  717. fetching a <b>Collection</b> from an API that returns the real data
  718. array wrapped in metadata:
  719. </p>
  720. <pre>
  721. {
  722. "page": 1,
  723. "limit": 10,
  724. "total": 2,
  725. "books": [
  726. {"id": 1, "title": "Pride and Prejudice"},
  727. {"id": 4, "title": "The Great Gatsby"}
  728. ]
  729. }
  730. </pre>
  731. <p>
  732. In the above example data, a <b>Collection</b> should populate using the
  733. <tt>"books"</tt> array rather than the root object structure. This
  734. difference is easily reconciled using a <tt>parse</tt> method that
  735. returns (or transforms) the desired portion of API data:
  736. </p>
  737. <pre>
  738. var Books = Backbone.Collection.extend({
  739. url: '/books',
  740. parse: function(data) {
  741. return data.books;
  742. }
  743. });
  744. </pre>
  745. <h2 id="View-rendering">View Rendering</h2>
  746. <img class="figure" src="docs/images/intro-views.svg" alt="View rendering.">
  747. <p>
  748. Each <b>View</b> manages the rendering and user interaction within its own
  749. DOM element. If you're strict about not allowing views to reach outside
  750. of themselves, it helps keep your interface flexible &mdash; allowing
  751. views to be rendered in isolation in any place where they might be needed.
  752. </p>
  753. <p>
  754. Backbone remains unopinionated about the process used to render <b>View</b>
  755. objects and their subviews into UI: you define how your models get translated
  756. into HTML (or SVG, or Canvas, or something even more exotic).
  757. It could be as prosaic as a simple
  758. <a href="http://underscorejs.org/#template">Underscore template</a>, or as fancy as the
  759. <a href="http://facebook.github.io/react/docs/tutorial.html">React virtual DOM</a>.
  760. Some basic approaches to rendering views can be found
  761. in the <a href="https://github.com/jashkenas/backbone/wiki/Backbone%2C-The-Primer">Backbone primer</a>.
  762. </p>
  763. <h2 id="Routing">Routing with URLs</h2>
  764. <img class="figure" src="docs/images/intro-routing.svg" alt="Routing">
  765. <p>
  766. In rich web applications, we still want to provide linkable,
  767. bookmarkable, and shareable URLs to meaningful locations within an app.
  768. Use the <b>Router</b> to update the browser URL whenever the user
  769. reaches a new "place" in your app that they might want to bookmark or share.
  770. Conversely, the <b>Router</b> detects changes to the URL &mdash; say,
  771. pressing the "Back" button &mdash; and can tell your application exactly where you
  772. are now.
  773. </p>
  774. <h2 id="Events">Backbone.Events</h2>
  775. <p>
  776. <b>Events</b> is a module that can be mixed in to any object, giving the
  777. object the ability to bind and trigger custom named events. Events do not
  778. have to be declared before they are bound, and may take passed arguments.
  779. For example:
  780. </p>
  781. <pre class="runnable">
  782. var object = {};
  783. _.extend(object, Backbone.Events);
  784. object.on("alert", function(msg) {
  785. alert("Triggered " + msg);
  786. });
  787. object.trigger("alert", "an event");
  788. </pre>
  789. <p>
  790. For example, to make a handy event dispatcher that can coordinate events
  791. among different areas of your application: <tt>var dispatcher = _.clone(Backbone.Events)</tt>
  792. </p>
  793. <p id="Events-on">
  794. <b class="header">on</b><code>object.on(event, callback, [context])</code><span class="alias">Alias: bind</span>
  795. <br />
  796. Bind a <b>callback</b> function to an object. The callback will be invoked
  797. whenever the <b>event</b> is fired.
  798. If you have a large number of different events on a page, the convention is to use colons to
  799. namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>.
  800. The event string may also be a space-delimited list of several events...
  801. </p>
  802. <pre>
  803. book.on("change:title change:author", ...);
  804. </pre>
  805. <p>
  806. To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
  807. pass the optional third argument: <tt>model.on('change', this.render, this)</tt>
  808. </p>
  809. <p>
  810. Callbacks bound to the special
  811. <tt>"all"</tt> event will be triggered when any event occurs, and are passed
  812. the name of the event as the first argument. For example, to proxy all events
  813. from one object to another:
  814. </p>
  815. <pre>
  816. proxy.on("all", function(eventName) {
  817. object.trigger(eventName);
  818. });
  819. </pre>
  820. <p>
  821. All Backbone event methods also support an event map syntax, as an alternative
  822. to positional arguments:
  823. </p>
  824. <pre>
  825. book.on({
  826. "change:title": titleView.update,
  827. "change:author": authorPane.update,
  828. "destroy": bookView.remove
  829. });
  830. </pre>
  831. <p id="Events-off">
  832. <b class="header">off</b><code>object.off([event], [callback], [context])</code><span class="alias">Alias: unbind</span>
  833. <br />
  834. Remove a previously-bound <b>callback</b> function from an object. If no
  835. <b>context</b> is specified, all of the versions of the callback with
  836. different contexts will be removed. If no
  837. callback is specified, all callbacks for the <b>event</b> will be
  838. removed. If no event is specified, callbacks for <i>all</i> events
  839. will be removed.
  840. </p>
  841. <pre>
  842. // Removes just the `onChange` callback.
  843. object.off("change", onChange);
  844. // Removes all "change" callbacks.
  845. object.off("change");
  846. // Removes the `onChange` callback for all events.
  847. object.off(null, onChange);
  848. // Removes all callbacks for `context` for all events.
  849. object.off(null, null, context);
  850. // Removes all callbacks on `object`.
  851. object.off();
  852. </pre>
  853. <p>
  854. Note that calling <tt>model.off()</tt>, for example, will indeed remove <i>all</i> events
  855. on the model &mdash; including events that Backbone uses for internal bookkeeping.
  856. </p>
  857. <p id="Events-trigger">
  858. <b class="header">trigger</b><code>object.trigger(event, [*args])</code>
  859. <br />
  860. Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
  861. Subsequent arguments to <b>trigger</b> will be passed along to the
  862. event callbacks.
  863. </p>
  864. <p id="Events-once">
  865. <b class="header">once</b><code>object.once(event, callback, [context])</code>
  866. <br />
  867. Just like <a href="#Events-on">on</a>, but causes the bound callback to fire
  868. only once before being removed. Handy for saying "the next time that X happens, do this".
  869. When multiple events are passed in using the space separated syntax, the event will fire once
  870. for every event you passed in, not once for a combination of all events
  871. </p>
  872. <p id="Events-listenTo">
  873. <b class="header">listenTo</b><code>object.listenTo(other, event, callback)</code>
  874. <br />
  875. Tell an <b>object</b> to listen to a particular event on an <b>other</b>
  876. object. The advantage of using this form, instead of <tt>other.on(event,
  877. callback, object)</tt>, is that <b>listenTo</b> allows the <b>object</b>
  878. to keep track of the events, and they can be removed all at once later
  879. on. The <b>callback</b> will always be called with <b>object</b> as
  880. context.
  881. </p>
  882. <pre>
  883. view.listenTo(model, 'change', view.render);
  884. </pre>
  885. <p id="Events-stopListening">
  886. <b class="header">stopListening</b><code>object.stopListening([other], [event], [callback])</code>
  887. <br />
  888. Tell an <b>object</b> to stop listening to events. Either call
  889. <b>stopListening</b> with no arguments to have the <b>object</b> remove
  890. all of its <a href="#Events-listenTo">registered</a> callbacks ... or be more
  891. precise by telling it to remove just the events it's listening to on a
  892. specific object, or a specific event, or just a specific callback.
  893. </p>
  894. <pre>
  895. view.stopListening();
  896. view.stopListening(model);
  897. </pre>
  898. <p id="Events-listenToOnce">
  899. <b class="header">listenToOnce</b><code>object.listenToOnce(other, event, callback)</code>
  900. <br />
  901. Just like <a href="#Events-listenTo">listenTo</a>, but causes the bound
  902. callback to fire only once before being removed.
  903. </p>
  904. <p id="Events-catalog">
  905. <b class="header">Catalog of Events</b>
  906. <br />
  907. Here's the complete list of built-in Backbone events, with arguments.
  908. You're also free to trigger your own events on Models, Collections and
  909. Views as you see fit. The <tt>Backbone</tt> object itself mixes in <tt>Events</tt>,
  910. and can be used to emit any global events that your application needs.
  911. </p>
  912. <ul class="small">
  913. <li><b>"add"</b> (model, collection, options) &mdash; when a model is added to a collection.</li>
  914. <li><b>"remove"</b> (model, collection, options) &mdash; when a model is removed from a collection.</li>
  915. <li><b>"update"</b> (collection, options) &mdash; single event triggered after any number of models have been added or removed from a collection.</li>
  916. <li><b>"reset"</b> (collection, options) &mdash; when the collection's entire contents have been replaced.</li>
  917. <li><b>"sort"</b> (collection, options) &mdash; when the collection has been re-sorted.</li>
  918. <li><b>"change"</b> (model, options) &mdash; when a model's attributes have changed.</li>
  919. <li><b>"change:[attribute]"</b> (model, value, options) &mdash; when a specific attribute has been updated.</li>
  920. <li><b>"destroy"</b> (model, collection, options) &mdash; when a model is <a href="#Model-destroy">destroyed</a>.</li>
  921. <li><b>"request"</b> (model_or_collection, xhr, options) &mdash; when a model or collection has started a request to the server.</li>
  922. <li><b>"sync"</b> (model_or_collection, resp, options) &mdash; when a model or collection has been successfully synced with the server.</li>
  923. <li><b>"error"</b> (model_or_collection, resp, options) &mdash; when a model's or collection's request to the server has failed.</li>
  924. <li><b>"invalid"</b> (model, error, options) &mdash; when a model's <a href="#Model-validate">validation</a> fails on the client.</li>
  925. <li><b>"route:[name]"</b> (params) &mdash; Fired by the router when a specific route is matched.</li>
  926. <li><b>"route"</b> (route, params) &mdash; Fired by the router when <i>any</i> route has been matched.</li>
  927. <li><b>"route"</b> (router, route, params) &mdash; Fired by history when <i>any</i> route has been matched.</li>
  928. <li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument.</li>
  929. </ul>
  930. <p>
  931. Generally speaking, when calling a function that emits an event
  932. (<tt>model.set</tt>, <tt>collection.add</tt>, and so on...),
  933. if you'd like to prevent the event from being triggered, you may pass
  934. <tt>{silent: true}</tt> as an option. Note that this is <i>rarely</i>,
  935. perhaps even never, a good idea. Passing through a specific flag
  936. in the options for your event callback to look at, and choose to ignore,
  937. will usually work out better.
  938. </p>
  939. <h2 id="Model">Backbone.Model</h2>
  940. <p>
  941. <b>Models</b> are the heart of any JavaScript application, containing
  942. the interactive data as well as a large part of the logic surrounding it:
  943. conversions, validations, computed properties, and access control. You
  944. extend <b>Backbone.Model</b> with your domain-specific methods, and
  945. <b>Model</b> provides a basic set of functionality for managing changes.
  946. </p>
  947. <p>
  948. The following is a contrived example, but it demonstrates defining a model
  949. with a custom method, setting an attribute, and firing an event keyed
  950. to changes in that specific attribute.
  951. After running this code once, <tt>sidebar</tt> will be
  952. available in your browser's console, so you can play around with it.
  953. </p>
  954. <pre class="runnable">
  955. var Sidebar = Backbone.Model.extend({
  956. promptColor: function() {
  957. var cssColor = prompt("Please enter a CSS color:");
  958. this.set({color: cssColor});
  959. }
  960. });
  961. window.sidebar = new Sidebar;
  962. sidebar.on('change:color', function(model, color) {
  963. $('#sidebar').css({background: color});
  964. });
  965. sidebar.set({color: 'white'});
  966. sidebar.promptColor();
  967. </pre>
  968. <p id="Model-extend">
  969. <b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
  970. <br />
  971. To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
  972. and provide instance <b>properties</b>, as well as optional
  973. <b>classProperties</b> to be attached directly to the constructor function.
  974. </p>
  975. <p>
  976. <b>extend</b> correctly sets up the prototype chain, so subclasses created
  977. with <b>extend</b> can be further extended and subclassed as far as you like.
  978. </p>
  979. <pre>
  980. var Note = Backbone.Model.extend({
  981. initialize: function() { ... },
  982. author: function() { ... },
  983. coordinates: function() { ... },
  984. allowedToEdit: function(account) {
  985. return true;
  986. }
  987. });
  988. var PrivateNote = Note.extend({
  989. allowedToEdit: function(account) {
  990. return account.owns(this);
  991. }
  992. });
  993. </pre>
  994. <p class="warning">
  995. Brief aside on <tt>super</tt>: JavaScript does not provide
  996. a simple way to call super &mdash; the function of the same name defined
  997. higher on the prototype chain. If you override a core function like
  998. <tt>set</tt>, or <tt>save</tt>, and you want to invoke the
  999. parent object's implementation, you'll have to explicitly call it, along these lines:
  1000. </p>
  1001. <pre>
  1002. var Note = Backbone.Model.extend({
  1003. set: function(attributes, options) {
  1004. Backbone.Model.prototype.set.apply(this, arguments);
  1005. ...
  1006. }
  1007. });
  1008. </pre>
  1009. <p id="Model-constructor">
  1010. <b class="header">constructor / initialize</b><code>new Model([attributes], [options])</code>
  1011. <br />
  1012. When creating an instance of a model, you can pass in the initial values
  1013. of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
  1014. model. If you define an <b>initialize</b> function, it will be invoked when
  1015. the model is created.
  1016. </p>
  1017. <pre>
  1018. new Book({
  1019. title: "One Thousand and One Nights",
  1020. author: "Scheherazade"
  1021. });
  1022. </pre>
  1023. <p>
  1024. In rare cases, if you're looking to get fancy,
  1025. you may want to override <b>constructor</b>, which allows
  1026. you to replace the actual constructor function for your model.
  1027. </p>
  1028. <pre>
  1029. var Library = Backbone.Model.extend({
  1030. constructor: function() {
  1031. this.books = new Books();
  1032. Backbone.Model.apply(this, arguments);
  1033. },
  1034. parse: function(data, options) {
  1035. this.books.reset(data.books);
  1036. return data.library;
  1037. }
  1038. });
  1039. </pre>
  1040. <p>
  1041. If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model
  1042. gains a <tt>collection</tt> property that will be used to indicate which
  1043. collection the model belongs to, and is used to help compute the model's
  1044. <a href="#Model-url">url</a>. The <tt>model.collection</tt> property is
  1045. normally created automatically when you first add a model to a collection.
  1046. Note that the reverse is not true, as passing this option to the constructor
  1047. will not automatically add the model to the collection. Useful, sometimes.
  1048. </p>
  1049. <p>
  1050. If <tt>{parse: true}</tt> is passed as an <b>option</b>, the <b>attributes</b>
  1051. will first be converted by <a href="#Model-parse">parse</a> before being
  1052. <a href="#Model-set">set</a> on the model.
  1053. </p>
  1054. <p id="Model-get">
  1055. <b class="header">get</b><code>model.get(attribute)</code>
  1056. <br />
  1057. Get the current value of an attribute from the model. For example:
  1058. <tt>note.get("title")</tt>
  1059. </p>
  1060. <p id="Model-set">
  1061. <b class="header">set</b><code>model.set(attributes, [options])</code>
  1062. <br />
  1063. Set a hash of attributes (one or many) on the model. If any of the attributes
  1064. change the model's state, a <tt>"change"</tt> event will be triggered on the model.
  1065. Change events for specific attributes are also triggered, and you can bind
  1066. to those as well, for example: <tt>change:title</tt>, and <tt>change:content</tt>.
  1067. You may also pass individual keys and values.
  1068. </p>
  1069. <pre>
  1070. note.set({title: "March 20", content: "In his eyes she eclipses..."});
  1071. book.set("title", "A Scandal in Bohemia");
  1072. </pre>
  1073. <p id="Model-escape">
  1074. <b class="header">escape</b><code>model.escape(attribute)</code>
  1075. <br />
  1076. Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
  1077. of a model's attribute. If you're interpolating data from the model into
  1078. HTML, using <b>escape</b> to retrieve attributes will prevent
  1079. <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
  1080. </p>
  1081. <pre class="runnable">
  1082. var hacker = new Backbone.Model({
  1083. name: "&lt;script&gt;alert('xss')&lt;/script&gt;"
  1084. });
  1085. alert(hacker.escape('name'));
  1086. </pre>
  1087. <p id="Model-has">
  1088. <b class="header">has</b><code>model.has(attribute)</code>
  1089. <br />
  1090. Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
  1091. value.
  1092. </p>
  1093. <pre>
  1094. if (note.has("title")) {
  1095. ...
  1096. }
  1097. </pre>
  1098. <p id="Model-unset">
  1099. <b class="header">unset</b><code>model.unset(attribute, [options])</code>
  1100. <br />
  1101. Remove an attribute by deleting it from the internal attributes hash.
  1102. Fires a <tt>"change"</tt> event unless <tt>silent</tt> is passed as an option.
  1103. </p>
  1104. <p id="Model-clear">
  1105. <b class="header">clear</b><code>model.clear([options])</code>
  1106. <br />
  1107. Removes all attributes from the model, including the <tt>id</tt> attribute. Fires a <tt>"change"</tt> event unless
  1108. <tt>silent</tt> is passed as an option.
  1109. </p>
  1110. <p id="Model-id">
  1111. <b class="header">id</b><code>model.id</code>
  1112. <br />
  1113. A special property of models, the <b>id</b> is an arbitrary string
  1114. (integer id or UUID). If you set the <b>id</b> in the
  1115. attributes hash, it will be copied onto the model as a direct property.
  1116. Models can be retrieved by id from collections, and the id is used to generate
  1117. model URLs by default.
  1118. </p>
  1119. <p id="Model-idAttribute">
  1120. <b class="header">idAttribute</b><code>model.idAttribute</code>
  1121. <br />
  1122. A model's unique identifier is stored under the <tt>id</tt> attribute.
  1123. If you're directly communicating with a backend (CouchDB, MongoDB) that uses
  1124. a different unique key, you may set a Model's <tt>idAttribute</tt> to
  1125. transparently map from that key to <tt>id</tt>.
  1126. <pre class="runnable">
  1127. var Meal = Backbone.Model.extend({
  1128. idAttribute: "_id"
  1129. });
  1130. var cake = new Meal({ _id: 1, name: "Cake" });
  1131. alert("Cake id: " + cake.id);
  1132. </pre>
  1133. </p>
  1134. <p id="Model-cid">
  1135. <b class="header">cid</b><code>model.cid</code>
  1136. <br />
  1137. A special property of models, the <b>cid</b> or client id is a unique identifier
  1138. automatically assigned to all models when they're first created. Client ids
  1139. are handy when the model has not yet been saved to the server, and does not
  1140. yet have its eventual true <b>id</b>, but already needs to be visible in the UI.
  1141. </p>
  1142. <p id="Model-attributes">
  1143. <b class="header">attributes</b><code>model.attributes</code>
  1144. <br />
  1145. The <b>attributes</b> property is the internal hash containing the model's
  1146. state &mdash; usually (but not necessarily) a form of the JSON object
  1147. representing the model data on the server. It's often a straightforward
  1148. serialization of a row from the database, but it could also be client-side
  1149. computed state.
  1150. </p>
  1151. <p>
  1152. Please use <a href="#Model-set">set</a> to update the <b>attributes</b>
  1153. instead of modifying them directly. If you'd like to retrieve and munge a
  1154. copy of the model's attributes, use <tt>_.clone(model.attributes)</tt>
  1155. instead.
  1156. </p>
  1157. <p class="warning">
  1158. Due to the fact that <a href="#Events">Events</a> accepts space separated
  1159. lists of events, attribute names should not include spaces.
  1160. </p>
  1161. <p id="Model-changed">
  1162. <b class="header">changed</b><code>model.changed</code>
  1163. <br />
  1164. The <b>changed</b> property is the internal hash containing all the attributes
  1165. that have changed since its last <a href="#Model-set">set</a>.
  1166. Please do not update <b>changed</b> directly since its state is internally maintained
  1167. by <a href="#Model-set">set</a>. A copy of <b>changed</b> can be acquired from
  1168. <a href="#Model-changedAttributes">changedAttributes</a>.
  1169. </p>
  1170. <p id="Model-defaults">
  1171. <b class="header">defaults</b><code>model.defaults or model.defaults()</code>
  1172. <br />
  1173. The <b>defaults</b> hash (or function) can be used to specify the default
  1174. attributes for your model. When creating an instance of the model,
  1175. any unspecified attributes will be set to their default value.
  1176. </p>
  1177. <pre class="runnable">
  1178. var Meal = Backbone.Model.extend({
  1179. defaults: {
  1180. "appetizer": "caesar salad",
  1181. "entree": "ravioli",
  1182. "dessert": "cheesecake"
  1183. }
  1184. });
  1185. alert("Dessert will be " + (new Meal).get('dessert'));
  1186. </pre>
  1187. <p class="warning">
  1188. Remember that in JavaScript, objects are passed by reference, so if you
  1189. include an object as a default value, it will be s

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