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