PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

http://github.com/documentcloud/backbone
HTML | 5260 lines | 4638 code | 622 blank | 0 comment | 0 complexity | 25c9461c9eb7dd9ad1f9429821ad8a79 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", "Lucida Sans Unicode", Helvetica, Arial, sans-serif;
  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. input#function_filter {
  65. width: 80%;
  66. }
  67. div.container {
  68. position: relative;
  69. width: 550px;
  70. margin: 40px 0 50px 260px;
  71. }
  72. img#logo {
  73. width: 450px;
  74. height: 80px;
  75. }
  76. div.run {
  77. position: absolute;
  78. right: 15px;
  79. width: 26px; height: 18px;
  80. background: url('docs/images/arrows.png') no-repeat -26px 0;
  81. }
  82. div.run:active {
  83. background-position: -51px 0;
  84. }
  85. p, div.container ul {
  86. margin: 25px 0;
  87. width: 550px;
  88. }
  89. p.warning {
  90. font-size: 12px;
  91. line-height: 18px;
  92. font-style: italic;
  93. }
  94. div.container ul {
  95. list-style: circle;
  96. padding-left: 15px;
  97. font-size: 13px;
  98. line-height: 18px;
  99. }
  100. div.container ul li {
  101. margin-bottom: 10px;
  102. }
  103. div.container ul.small {
  104. font-size: 12px;
  105. }
  106. a, a:visited {
  107. color: #444;
  108. }
  109. a:active, a:hover {
  110. color: #000;
  111. }
  112. a.punch {
  113. display: inline-block;
  114. background: #4162a8;
  115. border-top: 1px solid #38538c;
  116. border-right: 1px solid #1f2d4d;
  117. border-bottom: 1px solid #151e33;
  118. border-left: 1px solid #1f2d4d;
  119. -webkit-border-radius: 4px;
  120. -moz-border-radius: 4px;
  121. -ms-border-radius: 4px;
  122. -o-border-radius: 4px;
  123. border-radius: 4px;
  124. -webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  125. -moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  126. -ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  127. -o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  128. box-shadow: inset 0 1px 10px 1px #5c8bee, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  129. color: #fff;
  130. font: bold 14px "helvetica neue", helvetica, arial, sans-serif;
  131. line-height: 1;
  132. margin-bottom: 15px;
  133. padding: 8px 0 10px 0;
  134. text-align: center;
  135. text-shadow: 0px -1px 1px #1e2d4d;
  136. text-decoration: none;
  137. width: 225px;
  138. -webkit-background-clip: padding-box; }
  139. a.punch:hover {
  140. -webkit-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  141. -moz-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  142. -ms-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  143. -o-box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  144. box-shadow: inset 0 0px 20px 1px #87adff, 0px 1px 0 #1d2c4d, 0 6px 0px #1f3053, 0 8px 4px 1px #111111;
  145. cursor: pointer; }
  146. a.punch:active {
  147. -webkit-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  148. -moz-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  149. -ms-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  150. -o-box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  151. box-shadow: inset 0 1px 10px 1px #5c8bee, 0 1px 0 #1d2c4d, 0 2px 0 #1f3053, 0 4px 3px 0 #111111;
  152. margin-top: 5px; margin-bottom: 10px; }
  153. a img {
  154. border: 0;
  155. }
  156. a.travis-badge {
  157. display: block;
  158. }
  159. h1, h2, h3, h4, h5, h6 {
  160. padding-top: 20px;
  161. }
  162. h2 {
  163. font-size: 22px;
  164. }
  165. b.header {
  166. font-size: 18px;
  167. line-height: 35px;
  168. }
  169. span.alias {
  170. font-size: 14px;
  171. font-style: italic;
  172. margin-left: 20px;
  173. }
  174. table {
  175. margin: 15px 0 0; padding: 0;
  176. }
  177. tr, td {
  178. margin: 0; padding: 0;
  179. }
  180. td {
  181. padding: 0px 15px 5px 0;
  182. }
  183. table .rule {
  184. height: 1px;
  185. background: #ccc;
  186. margin: 5px 0;
  187. }
  188. code, pre, tt {
  189. font-family: Monaco, Consolas, "Lucida Console", monospace;
  190. font-size: 12px;
  191. line-height: 18px;
  192. font-style: normal;
  193. }
  194. tt {
  195. padding: 0px 3px;
  196. background: #fff;
  197. border: 1px solid #ddd;
  198. zoom: 1;
  199. }
  200. code {
  201. margin-left: 20px;
  202. }
  203. pre {
  204. font-size: 12px;
  205. padding: 2px 0 2px 15px;
  206. border: 4px solid #bbb; border-top: 0; border-bottom: 0;
  207. margin: 0px 0 25px;
  208. }
  209. img.example_image {
  210. margin: 0px auto;
  211. }
  212. img.example_retina {
  213. margin: 20px;
  214. box-shadow: 0 8px 15px rgba(0,0,0,0.4);
  215. }
  216. @media only screen and (-webkit-max-device-pixel-ratio: 1) and (max-width: 600px),
  217. only screen and (max--moz-device-pixel-ratio: 1) and (max-width: 600px) {
  218. div#sidebar {
  219. display: none;
  220. }
  221. img#logo {
  222. max-width: 450px;
  223. width: 100%;
  224. height: auto;
  225. }
  226. div.container {
  227. width: auto;
  228. margin-left: 15px;
  229. margin-right: 15px;
  230. }
  231. p, div.container ul {
  232. width: auto;
  233. }
  234. }
  235. @media only screen and (-webkit-min-device-pixel-ratio: 1.5) and (max-width: 640px),
  236. only screen and (-o-min-device-pixel-ratio: 3/2) and (max-width: 640px),
  237. only screen and (min-device-pixel-ratio: 1.5) and (max-width: 640px) {
  238. img {
  239. max-width: 100%;
  240. height: auto;
  241. }
  242. div#sidebar {
  243. -webkit-overflow-scrolling: initial;
  244. position: relative;
  245. width: 90%;
  246. height: 120px;
  247. left: 0;
  248. top: -7px;
  249. padding: 10px 0 10px 30px;
  250. border: 0;
  251. }
  252. img#logo {
  253. width: auto;
  254. height: auto;
  255. }
  256. div.container {
  257. margin: 0;
  258. width: 100%;
  259. }
  260. p, div.container ul {
  261. max-width: 98%;
  262. overflow-x: scroll;
  263. }
  264. table {
  265. position: relative;
  266. }
  267. tr:first-child td {
  268. padding-bottom: 25px;
  269. }
  270. td.text {
  271. line-height: 12px;
  272. padding: 0;
  273. position: absolute;
  274. left: 0;
  275. top: 48px;
  276. }
  277. tr:last-child td.text {
  278. top: 122px;
  279. }
  280. pre {
  281. overflow: scroll;
  282. }
  283. }
  284. img.figure {
  285. width: 100%;
  286. }
  287. div.columns {
  288. display: table;
  289. table-layout: fixed;
  290. width: 100%;
  291. }
  292. div.columns ul {
  293. margin: 10px 0;
  294. }
  295. div.col-50 {
  296. display: table-cell;
  297. width: 50%;
  298. }
  299. </style>
  300. </head>
  301. <body>
  302. <div id="sidebar" class="interface">
  303. <a class="toc_title" href="#">
  304. Backbone.js <span class="version">(1.4.0)</span>
  305. </a>
  306. <ul class="toc_section">
  307. <li>&raquo; <a href="http://github.com/jashkenas/backbone">GitHub Repository</a></li>
  308. <li>&raquo; <a href="docs/backbone.html">Annotated Source</a></li>
  309. </ul>
  310. <input id="function_filter" placeholder="Filter" type="text" autofocus />
  311. <div class="searchable_section">
  312. <a class="toc_title" href="#Getting-started">
  313. Getting Started
  314. </a>
  315. <ul class="toc_section">
  316. <li data-name="Introduction">- <a href="#Getting-started">Introduction</a></li>
  317. <li data-name="Models and Views"> <a href="#Model-View-separation">Models and Views</a></li>
  318. <li data-name="Collections"> <a href="#Model-Collections">Collections</a></li>
  319. <li data-name="API Integration"> <a href="#API-integration">API Integration</a></li>
  320. <li data-name="Rendering"> <a href="#View-rendering">Rendering</a></li>
  321. <li data-name="Routing"> <a href="#Routing">Routing</a></li>
  322. </ul>
  323. </div>
  324. <div class="searchable_section">
  325. <a class="toc_title" href="#Events">
  326. Events
  327. </a>
  328. <ul class="toc_section">
  329. <li data-name="on"> <a href="#Events-on">on</a></li>
  330. <li data-name="off"> <a href="#Events-off">off</a></li>
  331. <li data-name="trigger"> <a href="#Events-trigger">trigger</a></li>
  332. <li data-name="once"> <a href="#Events-once">once</a></li>
  333. <li data-name="listenTo"> <a href="#Events-listenTo">listenTo</a></li>
  334. <li data-name="stopListening"> <a href="#Events-stopListening">stopListening</a></li>
  335. <li data-name="listenToOnce"> <a href="#Events-listenToOnce">listenToOnce</a></li>
  336. <li data-name="Catalog of Built-in Events">- <a href="#Events-catalog"><b>Catalog of Built-in Events</b></a></li>
  337. </ul>
  338. </div>
  339. <div class="searchable_section">
  340. <a class="toc_title" href="#Model">
  341. Model
  342. </a>
  343. <ul class="toc_section">
  344. <li data-name="extend"> <a href="#Model-extend">extend</a></li>
  345. <li data-name="preinitialize"> <a href="#Model-preinitialize">preinitialize</a></li>
  346. <li data-name="constructor / initialize"> <a href="#Model-constructor">constructor / initialize</a></li>
  347. <li data-name="get"> <a href="#Model-get">get</a></li>
  348. <li data-name="set"> <a href="#Model-set">set</a></li>
  349. <li data-name="escape"> <a href="#Model-escape">escape</a></li>
  350. <li data-name="has"> <a href="#Model-has">has</a></li>
  351. <li data-name="unset"> <a href="#Model-unset">unset</a></li>
  352. <li data-name="clear"> <a href="#Model-clear">clear</a></li>
  353. <li data-name="id"> <a href="#Model-id">id</a></li>
  354. <li data-name="idAttribute"> <a href="#Model-idAttribute">idAttribute</a></li>
  355. <li data-name="cid"> <a href="#Model-cid">cid</a></li>
  356. <li data-name="attributes"> <a href="#Model-attributes">attributes</a></li>
  357. <li data-name="changed"> <a href="#Model-changed">changed</a></li>
  358. <li data-name="defaults"> <a href="#Model-defaults">defaults</a></li>
  359. <li data-name="toJSON"> <a href="#Model-toJSON">toJSON</a></li>
  360. <li data-name="sync"> <a href="#Model-sync">sync</a></li>
  361. <li data-name="fetch"> <a href="#Model-fetch">fetch</a></li>
  362. <li data-name="save"> <a href="#Model-save">save</a></li>
  363. <li data-name="destroy"> <a href="#Model-destroy">destroy</a></li>
  364. <li data-name="Underscore Methods"> <a href="#Model-Underscore-Methods"><b>Underscore Methods (9)</b></a></li>
  365. <li data-name="validate"> <a href="#Model-validate">validate</a></li>
  366. <li data-name="validationError"> <a href="#Model-validationError">validationError</a></li>
  367. <li data-name="isValid"> <a href="#Model-isValid">isValid</a></li>
  368. <li data-name="url"> <a href="#Model-url">url</a></li>
  369. <li data-name="urlRoot"> <a href="#Model-urlRoot">urlRoot</a></li>
  370. <li data-name="parse"> <a href="#Model-parse">parse</a></li>
  371. <li data-name="clone"> <a href="#Model-clone">clone</a></li>
  372. <li data-name="isNew"> <a href="#Model-isNew">isNew</a></li>
  373. <li data-name="hasChanged"> <a href="#Model-hasChanged">hasChanged</a></li>
  374. <li data-name="changedAttributes"> <a href="#Model-changedAttributes">changedAttributes</a></li>
  375. <li data-name="previous"> <a href="#Model-previous">previous</a></li>
  376. <li data-name="previousAttributes"> <a href="#Model-previousAttributes">previousAttributes</a></li>
  377. </ul>
  378. </div>
  379. <div class="searchable_section">
  380. <a class="toc_title" href="#Collection">
  381. Collection
  382. </a>
  383. <ul class="toc_section">
  384. <li data-name="extend"> <a href="#Collection-extend">extend</a></li>
  385. <li data-name="model"> <a href="#Collection-model">model</a></li>
  386. <li data-name="modelId"> <a href="#Collection-modelId">modelId</a></li>
  387. <li data-name="preinitialize" data-name="preinitialize"> <a href="#Collection-preinitialize">preinitialize</a></li>
  388. <li data-name="constructor / initialize" data-name="constructor / initialize"> <a href="#Collection-constructor">constructor / initialize</a></li>
  389. <li data-name="models"> <a href="#Collection-models">models</a></li>
  390. <li data-name="toJSON"> <a href="#Collection-toJSON">toJSON</a></li>
  391. <li data-name="sync"> <a href="#Collection-sync">sync</a></li>
  392. <li data-name="Underscore Methods"> <a href="#Collection-Underscore-Methods"><b>Underscore Methods (46)</b></a></li>
  393. <li data-name="add"> <a href="#Collection-add">add</a></li>
  394. <li data-name="remove"> <a href="#Collection-remove">remove</a></li>
  395. <li data-name="reset"> <a href="#Collection-reset">reset</a></li>
  396. <li data-name="set"> <a href="#Collection-set">set</a></li>
  397. <li data-name="get"> <a href="#Collection-get">get</a></li>
  398. <li data-name="at"> <a href="#Collection-at">at</a></li>
  399. <li data-name="push"> <a href="#Collection-push">push</a></li>
  400. <li data-name="pop"> <a href="#Collection-pop">pop</a></li>
  401. <li data-name="unshift"> <a href="#Collection-unshift">unshift</a></li>
  402. <li data-name="shift"> <a href="#Collection-shift">shift</a></li>
  403. <li data-name="slice"> <a href="#Collection-slice">slice</a></li>
  404. <li data-name="length"> <a href="#Collection-length">length</a></li>
  405. <li data-name="comparator"> <a href="#Collection-comparator">comparator</a></li>
  406. <li data-name="sort"> <a href="#Collection-sort">sort</a></li>
  407. <li data-name="pluck"> <a href="#Collection-pluck">pluck</a></li>
  408. <li data-name="where"> <a href="#Collection-where">where</a></li>
  409. <li data-name="findWhere"> <a href="#Collection-findWhere">findWhere</a></li>
  410. <li data-name="url"> <a href="#Collection-url">url</a></li>
  411. <li data-name="parse"> <a href="#Collection-parse">parse</a></li>
  412. <li data-name="clone"> <a href="#Collection-clone">clone</a></li>
  413. <li data-name="fetch"> <a href="#Collection-fetch">fetch</a></li>
  414. <li data-name="create"> <a href="#Collection-create">create</a></li>
  415. <li data-name="sync"> <a href="#Collection-mixin">mixin</a></li>
  416. </ul>
  417. </div>
  418. <div class="searchable_section">
  419. <a class="toc_title" href="#Router">
  420. Router
  421. </a>
  422. <ul class="toc_section">
  423. <li data-name="extend"> <a href="#Router-extend">extend</a></li>
  424. <li data-name="routes"> <a href="#Router-routes">routes</a></li>
  425. <li data-name="preinitialize"> <a href="#Router-preinitialize">preinitialize</a></li>
  426. <li data-name="constructor / initialize"> <a href="#Router-constructor">constructor / initialize</a></li>
  427. <li data-name="route"> <a href="#Router-route">route</a></li>
  428. <li data-name="navigate"> <a href="#Router-navigate">navigate</a></li>
  429. <li data-name="execute"> <a href="#Router-execute">execute</a></li>
  430. </ul>
  431. </div>
  432. <div class="searchable_section">
  433. <a class="toc_title" href="#History">
  434. History
  435. </a>
  436. <ul class="toc_section">
  437. <li data-name="start"> <a href="#History-start">start</a></li>
  438. </ul>
  439. </div>
  440. <div class="searchable_section">
  441. <a class="toc_title" href="#Sync">
  442. Sync
  443. </a>
  444. <ul class="toc_section">
  445. <li data-name="Backbone.sync"> <a href="#Sync">Backbone.sync</a></li>
  446. <li data-name="Backbone.ajax"> <a href="#Sync-ajax">Backbone.ajax</a></li>
  447. <li data-name="Backbone.emulateHTTP"> <a href="#Sync-emulateHTTP">Backbone.emulateHTTP</a></li>
  448. <li data-name="Backbone.emulateJSON"> <a href="#Sync-emulateJSON">Backbone.emulateJSON</a></li>
  449. </ul>
  450. </div>
  451. <div class="searchable_section">
  452. <a class="toc_title" href="#View">
  453. View
  454. </a>
  455. <ul class="toc_section">
  456. <li data-name="extend"> <a href="#View-extend">extend</a></li>
  457. <li data-name="preinitialize"> <a href="#View-preinitialize">preinitialize</a></li>
  458. <li data-name="constructor / initialize"> <a href="#View-constructor">constructor / initialize</a></li>
  459. <li data-name="el"> <a href="#View-el">el</a></li>
  460. <li data-name="$el"> <a href="#View-$el">$el</a></li>
  461. <li data-name="setElement"> <a href="#View-setElement">setElement</a></li>
  462. <li data-name="attributes"> <a href="#View-attributes">attributes</a></li>
  463. <li data-name="$ (jQuery)"> <a href="#View-dollar">$ (jQuery)</a></li>
  464. <li data-name="template"> <a href="#View-template">template</a></li>
  465. <li data-name="render"> <a href="#View-render">render</a></li>
  466. <li data-name="remove"> <a href="#View-remove">remove</a></li>
  467. <li data-name="events"> <a href="#View-events">events</a></li>
  468. <li data-name="delegateEvents"> <a href="#View-delegateEvents">delegateEvents</a></li>
  469. <li data-name="undelegateEvents"> <a href="#View-undelegateEvents">undelegateEvents</a></li>
  470. </ul>
  471. </div>
  472. <div class="searchable_section">
  473. <a class="toc_title" href="#Utility">
  474. Utility
  475. </a>
  476. <ul class="toc_section">
  477. <li data-name="Backbone.noConflict"> <a href="#Utility-Backbone-noConflict">Backbone.noConflict</a></li>
  478. <li data-name="Backbone.$"> <a href="#Utility-Backbone-$">Backbone.$</a></li>
  479. </ul>
  480. </div>
  481. <div class="searchable_section">
  482. <a class="toc_title" href="#faq">
  483. F.A.Q.
  484. </a>
  485. <ul class="toc_section">
  486. <li data-name="Why Backbone?"> <a href="#FAQ-why-backbone">Why Backbone?</a></li>
  487. <li data-name="More Than One Way To Do It"> <a href="#FAQ-tim-toady">More Than One Way To Do It</a></li>
  488. <li data-name="Nested Models and Collections"> <a href="#FAQ-nested">Nested Models &amp; Collections</a></li>
  489. <li data-name="Loading Bootstrapped Models"> <a href="#FAQ-bootstrap">Loading Bootstrapped Models</a></li>
  490. <li data-name="Extending Backbone"> <a href="#FAQ-extending">Extending Backbone</a></li>
  491. <li data-name="Traditional MVC"> <a href="#FAQ-mvc">Traditional MVC</a></li>
  492. <li data-name="Binding this"> <a href="#FAQ-this">Binding "this"</a></li>
  493. <li data-name="Working with Rails"> <a href="#FAQ-rails">Working with Rails</a></li>
  494. </ul>
  495. </div>
  496. <div class="searchable_section">
  497. <a class="toc_title" href="#examples">
  498. Examples
  499. </a>
  500. <ul class="toc_section">
  501. <li data-name="Todos"> <a href="#examples-todos">Todos</a></li>
  502. <li data-name="DocumentCloud"> <a href="#examples-documentcloud">DocumentCloud</a></li>
  503. <li data-name="USA Today"> <a href="#examples-usa-today">USA Today</a></li>
  504. <li data-name="Rdio"> <a href="#examples-rdio">Rdio</a></li>
  505. <li data-name="Hulu"> <a href="#examples-hulu">Hulu</a></li>
  506. <li data-name="Quartz"> <a href="#examples-quartz">Quartz</a></li>
  507. <li data-name="Earth"> <a href="#examples-earth">Earth</a></li>
  508. <li data-name="Vox"> <a href="#examples-vox">Vox</a></li>
  509. <li data-name="Gawker Media"> <a href="#examples-gawker">Gawker Media</a></li>
  510. <li data-name="Flow"> <a href="#examples-flow">Flow</a></li>
  511. <li data-name="Gilt Groupe"> <a href="#examples-gilt">Gilt Groupe</a></li>
  512. <li data-name="Enigma"> <a href="#examples-enigma">Enigma</a></li>
  513. <li data-name="NewsBlur"> <a href="#examples-newsblur">NewsBlur</a></li>
  514. <li data-name="WordPress.com"> <a href="#examples-wordpress">WordPress.com</a></li>
  515. <li data-name="Foursquare"> <a href="#examples-foursquare">Foursquare</a></li>
  516. <li data-name="Bitbucket"> <a href="#examples-bitbucket">Bitbucket</a></li>
  517. <li data-name="Disqus"> <a href="#examples-disqus">Disqus</a></li>
  518. <li data-name="Delicious"> <a href="#examples-delicious">Delicious</a></li>
  519. <li data-name="Khan Academy"> <a href="#examples-khan-academy">Khan Academy</a></li>
  520. <li data-name="IRCCloud"> <a href="#examples-irccloud">IRCCloud</a></li>
  521. <li data-name="Pitchfork"> <a href="#examples-pitchfork">Pitchfork</a></li>
  522. <li data-name="Spin"> <a href="#examples-spin">Spin</a></li>
  523. <li data-name="ZocDoc"> <a href="#examples-zocdoc">ZocDoc</a></li>
  524. <li data-name="Walmart Mobile"> <a href="#examples-walmart">Walmart Mobile</a></li>
  525. <li data-name="Groupon Now!"> <a href="#examples-groupon">Groupon Now!</a></li>
  526. <li data-name="Basecamp"> <a href="#examples-basecamp">Basecamp</a></li>
  527. <li data-name="Slavery Footprint"> <a href="#examples-slavery-footprint">Slavery Footprint</a></li>
  528. <li data-name="Stripe"> <a href="#examples-stripe">Stripe</a></li>
  529. <li data-name="Airbnb"> <a href="#examples-airbnb">Airbnb</a></li>
  530. <li data-name="SoundCloud Mobile"> <a href="#examples-soundcloud">SoundCloud Mobile</a></li>
  531. <li data-name="Art.sy">- <a href="#examples-artsy">Art.sy</a></li>
  532. <li data-name="Pandora"> <a href="#examples-pandora">Pandora</a></li>
  533. <li data-name="Inkling"> <a href="#examples-inkling">Inkling</a></li>
  534. <li data-name="Code School"> <a href="#examples-code-school">Code School</a></li>
  535. <li data-name="CloudApp"> <a href="#examples-cloudapp">CloudApp</a></li>
  536. <li data-name="SeatGeek"> <a href="#examples-seatgeek">SeatGeek</a></li>
  537. <li data-name="Easel"> <a href="#examples-easel">Easel</a></li>
  538. <li data-name="Jolicloud">- <a href="#examples-jolicloud">Jolicloud</a></li>
  539. <li data-name="Salon.io"> <a href="#examples-salon">Salon.io</a></li>
  540. <li data-name="TileMill"> <a href="#examples-tilemill">TileMill</a></li>
  541. <li data-name="Blossom"> <a href="#examples-blossom">Blossom</a></li>
  542. <li data-name="Trello"> <a href="#examples-trello">Trello</a></li>
  543. <li data-name="Tzigla"> <a href="#examples-tzigla">Tzigla</a></li>
  544. </ul>
  545. </div>
  546. <div class="searchable_section">
  547. <a class="toc_title" href="#changelog">
  548. Change Log
  549. </a>
  550. </div>
  551. </div>
  552. <div class="container">
  553. <p>
  554. <img id="logo" src="docs/images/backbone.png" alt="Backbone.js" />
  555. </p>
  556. <p>
  557. Backbone.js gives structure to web applications
  558. by providing <b>models</b> with key-value binding and custom events,
  559. <b>collections</b> with a rich API of enumerable functions,
  560. <b>views</b> with declarative event handling, and connects it all to your
  561. existing API over a RESTful JSON interface.
  562. </p>
  563. <p>
  564. The project is <a href="http://github.com/jashkenas/backbone/">hosted on GitHub</a>,
  565. and the <a href="docs/backbone.html">annotated source code</a> is available,
  566. as well as an online <a href="test/">test suite</a>,
  567. an <a href="examples/todos/index.html">example application</a>,
  568. a <a href="https://github.com/jashkenas/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites">list of tutorials</a>
  569. and a <a href="#examples">long list of real-world projects</a> that use Backbone.
  570. Backbone is available for use under the <a href="http://github.com/jashkenas/backbone/blob/master/LICENSE">MIT software license</a>.
  571. </p>
  572. <p>
  573. You can report bugs and discuss features on the
  574. <a href="http://github.com/jashkenas/backbone/issues">GitHub issues page</a>,
  575. or add pages to the <a href="https://github.com/jashkenas/backbone/wiki">wiki</a>.
  576. </p>
  577. <p>
  578. <i>
  579. Backbone is an open-source component of
  580. <a href="http://documentcloud.org/">DocumentCloud</a>.
  581. </i>
  582. </p>
  583. <h2 id="downloads">
  584. Downloads &amp; Dependencies
  585. <span style="padding-left: 7px; font-size:11px; font-weight: normal;" class="interface">(Right-click, and use "Save As")</span>
  586. </h2>
  587. <table>
  588. <tr>
  589. <td><a class="punch" href="backbone.js">Development Version (1.4.0)</a></td>
  590. <td class="text"><i>72kb, Full source, tons of comments</i></td>
  591. </tr>
  592. <tr>
  593. <td><a class="punch" href="backbone-min.js">Production Version (1.4.0)</a></td>
  594. <td class="text" style="line-height: 16px;">
  595. <i>7.9kb, Packed and gzipped</i><br />
  596. <small>(<a href="backbone-min.map">Source Map</a>)</small>
  597. </td>
  598. </tr>
  599. <tr>
  600. <td><a class="punch" href="https://raw.github.com/jashkenas/backbone/master/backbone.js">Edge Version (master)</a></td>
  601. <td>
  602. <i>Unreleased, use at your own risk</i>
  603. </td>
  604. </tr>
  605. </table>
  606. <p>
  607. Backbone's only hard dependency is
  608. <a href="http://underscorejs.org/">Underscore.js</a> <small>( >= 1.8.3)</small>.
  609. For RESTful persistence and DOM manipulation with <a href="#View">Backbone.View</a>,
  610. include <b><a href="https://jquery.com/">jQuery</a></b> ( >= 1.11.0).
  611. <i>(Mimics of the Underscore and jQuery APIs, such as
  612. <a href="https://lodash.com/">Lodash</a> and
  613. <a href="http://zeptojs.com/">Zepto</a>, will
  614. also tend to work, with varying degrees of compatibility.)</i>
  615. </p>
  616. <h2 id="Getting-started">Getting Started</h2>
  617. <p>
  618. When working on a web application that involves a lot of JavaScript, one
  619. of the first things you learn is to stop tying your data to the DOM. It's all
  620. too easy to create JavaScript applications that end up as tangled piles of
  621. jQuery selectors and callbacks, all trying frantically to keep data in
  622. sync between the HTML UI, your JavaScript logic, and the database on your
  623. server. For rich client-side applications, a more structured approach
  624. is often helpful.
  625. </p>
  626. <p>
  627. With Backbone, you represent your data as
  628. <a href="#Model">Models</a>, which can be created, validated, destroyed,
  629. and saved to the server. Whenever a UI action causes an attribute of
  630. a model to change, the model triggers a <i>"change"</i> event; all
  631. the <a href="#View">Views</a> that display the model's state can be notified of the
  632. change, so that they are able to respond accordingly, re-rendering themselves with
  633. the new information. In a finished Backbone app, you don't have to write the glue
  634. code that looks into the DOM to find an element with a specific <i>id</i>,
  635. and update the HTML manually
  636. &mdash; when the model changes, the views simply update themselves.
  637. </p>
  638. <p>
  639. Philosophically, Backbone is an attempt to discover the minimal set
  640. of data-structuring (models and collections) and user interface (views
  641. and URLs) primitives that are generally useful when building web applications with
  642. JavaScript. In an ecosystem where overarching, decides-everything-for-you
  643. frameworks are commonplace, and many libraries require your site to be
  644. reorganized to suit their look, feel, and default behavior Backbone should
  645. continue to be a tool that gives you the <i>freedom</i> to design the full
  646. experience of your web application.
  647. </p>
  648. <p>
  649. If you're new here, and aren't yet quite sure what Backbone is for, start by
  650. browsing the <a href="#examples">list of Backbone-based projects</a>.
  651. </p>
  652. <p>
  653. Many of the code examples in this documentation are runnable, because
  654. Backbone is included on this page.
  655. Click the <i>play</i> button to execute them.
  656. </p>
  657. <h2 id="Model-View-separation">Models and Views</h2>
  658. <img class="figure" src="docs/images/intro-model-view.svg" alt="Model-View Separation.">
  659. <p>
  660. The single most important thing that Backbone can help you with is keeping
  661. your business logic separate from your user interface. When the two are
  662. entangled, change is hard; when logic doesn't depend on UI, your
  663. interface becomes easier to work with.
  664. </p>
  665. <div class="columns">
  666. <div class="col-50">
  667. <b>Model</b>
  668. <ul>
  669. <li>Orchestrates data and business logic.</li>
  670. <li>Loads and saves data from the server.</li>
  671. <li>Emits events when data changes.</li>
  672. </ul>
  673. </div>
  674. <div class="col-50">
  675. <b>View</b>
  676. <ul>
  677. <li>Listens for changes and renders UI.</li>
  678. <li>Handles user input and interactivity.</li>
  679. <li>Sends captured input to the model.</li>
  680. </ul>
  681. </div>
  682. </div>
  683. <p>
  684. A <b>Model</b> manages an internal table of data attributes, and
  685. triggers <tt>"change"</tt> events when any of its data is modified.
  686. Models handle syncing data with a persistence layer usually a REST API
  687. with a backing database. Design your models as the atomic reusable objects
  688. containing all of the helpful functions for manipulating their particular
  689. bit of data. Models should be able to be passed around throughout your app,
  690. and used anywhere that bit of data is needed.
  691. </p>
  692. <p>
  693. A <b>View</b> is an atomic chunk of user interface. It often renders the
  694. data from a specific model, or number of models &mdash; but views can
  695. also be data-less chunks of UI that stand alone.
  696. Models should be generally unaware of views. Instead, views listen to
  697. the model <tt>"change"</tt> events, and react or re-render themselves
  698. appropriately.
  699. </p>
  700. <h2 id="Model-Collections">Collections</h2>
  701. <img class="figure" src="docs/images/intro-collections.svg" alt="Model Collections.">
  702. <p>
  703. A <b>Collection</b> helps you deal with a group of related models, handling
  704. the loading and saving of new models to the server and providing helper
  705. functions for performing aggregations or computations against a list of models.
  706. Aside from their own events, collections also proxy through all of the
  707. events that occur to models within them, allowing you to listen in one place
  708. for any change that might happen to any model in the collection.
  709. </p>
  710. <h2 id="API-integration">API Integration</h2>
  711. <p>
  712. Backbone is pre-configured to sync with a RESTful API. Simply create a
  713. new Collection with the <tt>url</tt> of your resource endpoint:
  714. </p>
  715. <pre>
  716. var Books = Backbone.Collection.extend({
  717. url: '/books'
  718. });
  719. </pre>
  720. <p>
  721. The <b>Collection</b> and <b>Model</b> components together form a direct
  722. mapping of REST resources using the following methods:
  723. </p>
  724. <pre>
  725. GET /books/ .... collection.fetch();
  726. POST /books/ .... collection.create();
  727. GET /books/1 ... model.fetch();
  728. PUT /books/1 ... model.save();
  729. DEL /books/1 ... model.destroy();
  730. </pre>
  731. <p>
  732. When fetching raw JSON data from an API, a <b>Collection</b> will
  733. automatically populate itself with data formatted as an array, while
  734. a <b>Model</b> will automatically populate itself with data formatted
  735. as an object:
  736. </p>
  737. <pre>
  738. [{"id": 1}] ..... populates a Collection with one model.
  739. {"id": 1} ....... populates a Model with one attribute.
  740. </pre>
  741. <p>
  742. However, it's fairly common to encounter APIs that return data in a
  743. different format than what Backbone expects. For example, consider
  744. fetching a <b>Collection</b> from an API that returns the real data
  745. array wrapped in metadata:
  746. </p>
  747. <pre>
  748. {
  749. "page": 1,
  750. "limit": 10,
  751. "total": 2,
  752. "books": [
  753. {"id": 1, "title": "Pride and Prejudice"},
  754. {"id": 4, "title": "The Great Gatsby"}
  755. ]
  756. }
  757. </pre>
  758. <p>
  759. In the above example data, a <b>Collection</b> should populate using the
  760. <tt>"books"</tt> array rather than the root object structure. This
  761. difference is easily reconciled using a <tt>parse</tt> method that
  762. returns (or transforms) the desired portion of API data:
  763. </p>
  764. <pre>
  765. var Books = Backbone.Collection.extend({
  766. url: '/books',
  767. parse: function(data) {
  768. return data.books;
  769. }
  770. });
  771. </pre>
  772. <h2 id="View-rendering">View Rendering</h2>
  773. <img class="figure" src="docs/images/intro-views.svg" alt="View rendering.">
  774. <p>
  775. Each <b>View</b> manages the rendering and user interaction within its own
  776. DOM element. If you're strict about not allowing views to reach outside
  777. of themselves, it helps keep your interface flexible &mdash; allowing
  778. views to be rendered in isolation in any place where they might be needed.
  779. </p>
  780. <p>
  781. Backbone remains unopinionated about the process used to render <b>View</b>
  782. objects and their subviews into UI: you define how your models get translated
  783. into HTML (or SVG, or Canvas, or something even more exotic).
  784. It could be as prosaic as a simple
  785. <a href="http://underscorejs.org/#template">Underscore template</a>, or as fancy as the
  786. <a href="http://facebook.github.io/react/docs/tutorial.html">React virtual DOM</a>.
  787. Some basic approaches to rendering views can be found
  788. in the <a href="https://github.com/jashkenas/backbone/wiki/Backbone%2C-The-Primer">Backbone primer</a>.
  789. </p>
  790. <h2 id="Routing">Routing with URLs</h2>
  791. <img class="figure" src="docs/images/intro-routing.svg" alt="Routing">
  792. <p>
  793. In rich web applications, we still want to provide linkable,
  794. bookmarkable, and shareable URLs to meaningful locations within an app.
  795. Use the <b>Router</b> to update the browser URL whenever the user
  796. reaches a new "place" in your app that they might want to bookmark or share.
  797. Conversely, the <b>Router</b> detects changes to the URL &mdash; say,
  798. pressing the "Back" button &mdash; and can tell your application exactly where you
  799. are now.
  800. </p>
  801. <h2 id="Events">Backbone.Events</h2>
  802. <p>
  803. <b>Events</b> is a module that can be mixed in to any object, giving the
  804. object the ability to bind and trigger custom named events. Events do not
  805. have to be declared before they are bound, and may take passed arguments.
  806. For example:
  807. </p>
  808. <pre class="runnable">
  809. var object = {};
  810. _.extend(object, Backbone.Events);
  811. object.on("alert", function(msg) {
  812. alert("Triggered " + msg);
  813. });
  814. object.trigger("alert", "an event");
  815. </pre>
  816. <p>
  817. For example, to make a handy event dispatcher that can coordinate events
  818. among different areas of your application: <tt>var dispatcher = _.clone(Backbone.Events)</tt>
  819. </p>
  820. <p id="Events-on">
  821. <b class="header">on</b><code>object.on(event, callback, [context])</code><span class="alias">Alias: bind</span>
  822. <br />
  823. Bind a <b>callback</b> function to an object. The callback will be invoked
  824. whenever the <b>event</b> is fired.
  825. If you have a large number of different events on a page, the convention is to use colons to
  826. namespace them: <tt>"poll:start"</tt>, or <tt>"change:selection"</tt>.
  827. The event string may also be a space-delimited list of several events...
  828. </p>
  829. <pre>
  830. book.on("change:title change:author", ...);
  831. </pre>
  832. <p>
  833. Callbacks bound to the special
  834. <tt>"all"</tt> event will be triggered when any event occurs, and are passed
  835. the name of the event as the first argument. For example, to proxy all events
  836. from one object to another:
  837. </p>
  838. <pre>
  839. proxy.on("all", function(eventName) {
  840. object.trigger(eventName);
  841. });
  842. </pre>
  843. <p>
  844. All Backbone event methods also support an event map syntax, as an alternative
  845. to positional arguments:
  846. </p>
  847. <pre>
  848. book.on({
  849. "change:author": authorPane.update,
  850. "change:title change:subtitle": titleView.update,
  851. "destroy": bookView.remove
  852. });
  853. </pre>
  854. <p>
  855. To supply a <b>context</b> value for <tt>this</tt> when the callback is invoked,
  856. pass the optional last argument: <tt>model.on('change', this.render, this)</tt> or
  857. <tt>model.on({change: this.render}, this)</tt>.
  858. </p>
  859. <p id="Events-off">
  860. <b class="header">off</b><code>object.off([event], [callback], [context])</code><span class="alias">Alias: unbind</span>
  861. <br />
  862. Remove a previously-bound <b>callback</b> function from an object. If no
  863. <b>context</b> is specified, all of the versions of the callback with
  864. different contexts will be removed. If no
  865. callback is specified, all callbacks for the <b>event</b> will be
  866. removed. If no event is specified, callbacks for <i>all</i> events
  867. will be removed.
  868. </p>
  869. <pre>
  870. // Removes just the `onChange` callback.
  871. object.off("change", onChange);
  872. // Removes all "change" callbacks.
  873. object.off("change");
  874. // Removes the `onChange` callback for all events.
  875. object.off(null, onChange);
  876. // Removes all callbacks for `context` for all events.
  877. object.off(null, null, context);
  878. // Removes all callbacks on `object`.
  879. object.off();
  880. </pre>
  881. <p>
  882. Note that calling <tt>model.off()</tt>, for example, will indeed remove <i>all</i> events
  883. on the model &mdash; including events that Backbone uses for internal bookkeeping.
  884. </p>
  885. <p id="Events-trigger">
  886. <b class="header">trigger</b><code>object.trigger(event, [*args])</code>
  887. <br />
  888. Trigger callbacks for the given <b>event</b>, or space-delimited list of events.
  889. Subsequent arguments to <b>trigger</b> will be passed along to the
  890. event callbacks.
  891. </p>
  892. <p id="Events-once">
  893. <b class="header">once</b><code>object.once(event, callback, [context])</code>
  894. <br />
  895. Just like <a href="#Events-on">on</a>, but causes the bound callback to fire
  896. only once before being removed. Handy for saying "the next time that X happens, do this".
  897. When multiple events are passed in using the space separated syntax, the event will fire once
  898. for every event you passed in, not once for a combination of all events
  899. </p>
  900. <p id="Events-listenTo">
  901. <b class="header">listenTo</b><code>object.listenTo(other, event, callback)</code>
  902. <br />
  903. Tell an <b>object</b> to listen to a particular event on an <b>other</b>
  904. object. The advantage of using this form, instead of <tt>other.on(event,
  905. callback, object)</tt>, is that <b>listenTo</b> allows the <b>object</b>
  906. to keep track of the events, and they can be removed all at once later
  907. on. The <b>callback</b> will always be called with <b>object</b> as
  908. context.
  909. </p>
  910. <pre>
  911. view.listenTo(model, 'change', view.render);
  912. </pre>
  913. <p id="Events-stopListening">
  914. <b class="header">stopListening</b><code>object.stopListening([other], [event], [callback])</code>
  915. <br />
  916. Tell an <b>object</b> to stop listening to events. Either call
  917. <b>stopListening</b> with no arguments to have the <b>object</b> remove
  918. all of its <a href="#Events-listenTo">registered</a> callbacks ... or be more
  919. precise by telling it to remove just the events it's listening to on a
  920. specific object, or a specific event, or just a specific callback.
  921. </p>
  922. <pre>
  923. view.stopListening();
  924. view.stopListening(model);
  925. </pre>
  926. <p id="Events-listenToOnce">
  927. <b class="header">listenToOnce</b><code>object.listenToOnce(other, event, callback)</code>
  928. <br />
  929. Just like <a href="#Events-listenTo">listenTo</a>, but causes the bound
  930. callback to fire only once before being removed.
  931. </p>
  932. <p id="Events-catalog">
  933. <b class="header">Catalog of Events</b>
  934. <br />
  935. Here's the complete list of built-in Backbone events, with arguments.
  936. You're also free to trigger your own events on Models, Collections and
  937. Views as you see fit. The <tt>Backbone</tt> object itself mixes in <tt>Events</tt>,
  938. and can be used to emit any global events that your application needs.
  939. </p>
  940. <ul class="small">
  941. <li><b>"add"</b> (model, collection, options) &mdash; when a model is added to a collection.</li>
  942. <li><b>"remove"</b> (model, collection, options) &mdash; when a model is removed from a collection.</li>
  943. <li><b>"update"</b> (collection, options) &mdash; single event triggered after any number of models have been added, removed or changed in a collection.</li>
  944. <li><b>"reset"</b> (collection, options) &mdash; when the collection's entire contents have been <a href="#Collection-reset">reset</a>.</li>
  945. <li><b>"sort"</b> (collection, options) &mdash; when the collection has been re-sorted.</li>
  946. <li><b>"change"</b> (model, options) &mdash; when a model's attributes have changed.</li>
  947. <li><b>"change:[attribute]"</b> (model, value, options) &mdash; when a specific attribute has been updated.</li>
  948. <li><b>"destroy"</b> (model, collection, options) &mdash; when a model is <a href="#Model-destroy">destroyed</a>.</li>
  949. <li><b>"request"</b> (model_or_collection, xhr, options) &mdash; when a model or collection has started a request to the server.</li>
  950. <li><b>"sync"</b> (model_or_collection, response, options) &mdash; when a model or collection has been successfully synced with the server.</li>
  951. <li><b>"error"</b> (model_or_collection, xhr, options) &mdash; when a model's or collection's request to the server has failed.</li>
  952. <li><b>"invalid"</b> (model, error, options) &mdash; when a model's <a href="#Model-validate">validation</a> fails on the client.</li>
  953. <li><b>"route:[name]"</b> (params) &mdash; Fired by the router when a specific route is matched.</li>
  954. <li><b>"route"</b> (route, params) &mdash; Fired by the router when <i>any</i> route has been matched.</li>
  955. <li><b>"route"</b> (router, route, params) &mdash; Fired by history when <i>any</i> route has been matched.</li>
  956. <li><b>"all"</b> &mdash; this special event fires for <i>any</i> triggered event, passing the event name as the first argument followed by all trigger arguments.</li>
  957. </ul>
  958. <p>
  959. Generally speaking, when calling a function that emits an event
  960. (<tt>model.set</tt>, <tt>collection.add</tt>, and so on...),
  961. if you'd like to prevent the event from being triggered, you may pass
  962. <tt>{silent: true}</tt> as an option. Note that this is <i>rarely</i>,
  963. perhaps even never, a good idea. Passing through a specific flag
  964. in the options for your event callback to look at, and choose to ignore,
  965. will usually work out better.
  966. </p>
  967. <h2 id="Model">Backbone.Model</h2>
  968. <p>
  969. <b>Models</b> are the heart of any JavaScript application, containing
  970. the interactive data as well as a large part of the logic surrounding it:
  971. conversions, validations, computed properties, and access control. You
  972. extend <b>Backbone.Model</b> with your domain-specific methods, and
  973. <b>Model</b> provides a basic set of functionality for managing changes.
  974. </p>
  975. <p>
  976. The following is a contrived example, but it demonstrates defining a model
  977. with a custom method, setting an attribute, and firing an event keyed
  978. to changes in that specific attribute.
  979. After running this code once, <tt>sidebar</tt> will be
  980. available in your browser's console, so you can play around with it.
  981. </p>
  982. <pre class="runnable">
  983. var Sidebar = Backbone.Model.extend({
  984. promptColor: function() {
  985. var cssColor = prompt("Please enter a CSS color:");
  986. this.set({color: cssColor});
  987. }
  988. });
  989. window.sidebar = new Sidebar;
  990. sidebar.on('change:color', function(model, color) {
  991. $('#sidebar').css({background: color});
  992. });
  993. sidebar.set({color: 'white'});
  994. sidebar.promptColor();
  995. </pre>
  996. <p id="Model-extend">
  997. <b class="header">extend</b><code>Backbone.Model.extend(properties, [classProperties])</code>
  998. <br />
  999. To create a <b>Model</b> class of your own, you extend <b>Backbone.Model</b>
  1000. and provide instance <b>properties</b>, as well as optional
  1001. <b>classProperties</b> to be attached directly to the constructor function.
  1002. </p>
  1003. <p>
  1004. <b>extend</b> correctly sets up the prototype chain, so subclasses created
  1005. with <b>extend</b> can be further extended and subclassed as far as you like.
  1006. </p>
  1007. <pre>
  1008. var Note = Backbone.Model.extend({
  1009. initialize: function() { ... },
  1010. author: function() { ... },
  1011. coordinates: function() { ... },
  1012. allowedToEdit: function(account) {
  1013. return true;
  1014. }
  1015. });
  1016. var PrivateNote = Note.extend({
  1017. allowedToEdit: function(account) {
  1018. return account.owns(this);
  1019. }
  1020. });
  1021. </pre>
  1022. <p class="warning">
  1023. Brief aside on <tt>super</tt>: JavaScript does not provide
  1024. a simple way to call super &mdash; the function of the same name defined
  1025. higher on the prototype chain. If you override a core function like
  1026. <tt>set</tt>, or <tt>save</tt>, and you want to invoke the
  1027. parent object's implementation, you'll have to explicitly call it, along these lines:
  1028. </p>
  1029. <pre>
  1030. var Note = Backbone.Model.extend({
  1031. set: function(attributes, options) {
  1032. Backbone.Model.prototype.set.apply(this, arguments);
  1033. ...
  1034. }
  1035. });
  1036. </pre>
  1037. <p id="Model-preinitialize">
  1038. <b class="header">preinitialize</b><code>new Model([attributes], [options])</code>
  1039. <br />
  1040. For use with models as ES classes. If you define a <b>preinitialize</b>
  1041. method, it will be invoked when the Model is first created, before any
  1042. instantiation logic is run for the Model.
  1043. </p>
  1044. <pre>
  1045. class Country extends Backbone.Model {
  1046. preinitialize({countryCode}) {
  1047. this.name = COUNTRY_NAMES[countryCode];
  1048. }
  1049. initialize() { ... }
  1050. }
  1051. </pre>
  1052. <p id="Model-constructor">
  1053. <b class="header">constructor / initialize</b><code>new Model([attributes], [options])</code>
  1054. <br />
  1055. When creating an instance of a model, you can pass in the initial values
  1056. of the <b>attributes</b>, which will be <a href="#Model-set">set</a> on the
  1057. model. If you define an <b>initialize</b> function, it will be invoked when
  1058. the model is created.
  1059. </p>
  1060. <pre>
  1061. new Book({
  1062. title: "One Thousand and One Nights",
  1063. author: "Scheherazade"
  1064. });
  1065. </pre>
  1066. <p>
  1067. In rare cases, if you're looking to get fancy,
  1068. you may want to override <b>constructor</b>, which allows
  1069. you to replace the actual constructor function for your model.
  1070. </p>
  1071. <pre>
  1072. var Library = Backbone.Model.extend({
  1073. constructor: function() {
  1074. this.books = new Books();
  1075. Backbone.Model.apply(this, arguments);
  1076. },
  1077. parse: function(data, options) {
  1078. this.books.reset(data.books);
  1079. return data.library;
  1080. }
  1081. });
  1082. </pre>
  1083. <p>
  1084. If you pass a <tt>{collection: ...}</tt> as the <b>options</b>, the model
  1085. gains a <tt>collection</tt> property that will be used to indicate which
  1086. collection the model belongs to, and is used to help compute the model's
  1087. <a href="#Model-url">url</a>. The <tt>model.collection</tt> property is
  1088. normally created automatically when you first add a model to a collection.
  1089. Note that the reverse is not true, as passing this option to the constructor
  1090. will not automatically add the model to the collection. Useful, sometimes.
  1091. </p>
  1092. <p>
  1093. If <tt>{parse: true}</tt> is passed as an <b>option</b>, the <b>attributes</b>
  1094. will first be converted by <a href="#Model-parse">parse</a> before being
  1095. <a href="#Model-set">set</a> on the model.
  1096. </p>
  1097. <p id="Model-get">
  1098. <b class="header">get</b><code>model.get(attribute)</code>
  1099. <br />
  1100. Get the current value of an attribute from the model. For example:
  1101. <tt>note.get("title")</tt>
  1102. </p>
  1103. <p id="Model-set">
  1104. <b class="header">set</b><code>model.set(attributes, [options])</code>
  1105. <br />
  1106. Set a hash of attributes (one or many) on the model. If any of the attributes
  1107. change the model's state, a <tt>"change"</tt> event will be triggered on the model.
  1108. Change events for specific attributes are also triggered, and you can b

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