153
153
< li > – < a href ="#Model-id "> id</ a > </ li >
154
154
< li > – < a href ="#Model-cid "> cid</ a > </ li >
155
155
< li > – < a href ="#Model-attributes "> attributes</ a > </ li >
156
+ < li > - < a href ="#Model-toJSON "> toJSON</ a > </ li >
156
157
< li > – < a href ="#Model-save "> save</ a > </ li >
157
158
< li > – < a href ="#Model-destroy "> destroy</ a > </ li >
158
159
< li > – < a href ="#Model-validate "> validate</ a > </ li >
171
172
</ a >
172
173
< ul class ="toc_section ">
173
174
< li > – < a href ="#Collection-extend "> extend</ a > </ li >
175
+ < li > – < a href ="#Collection-models "> models</ a > </ li >
174
176
< li > – < a href ="#Collection-Underscore-Methods "> < b > Underscore Methods (24)</ b > </ a > </ li >
175
177
< li > – < a href ="#Collection-add "> add</ a > </ li >
176
178
< li > – < a href ="#Collection-remove "> remove</ a > </ li >
177
179
< li > – < a href ="#Collection-get "> get</ a > </ li >
178
180
< li > – < a href ="#Collection-getByCid "> getByCid</ a > </ li >
179
181
< li > – < a href ="#Collection-at "> at</ a > </ li >
182
+ < li > – < a href ="#Collection-comparator "> comparator</ a > </ li >
180
183
< li > – < a href ="#Collection-sort "> sort</ a > </ li >
181
184
< li > – < a href ="#Collection-pluck "> pluck</ a > </ li >
182
185
< li > – < a href ="#Model-url "> url</ a > </ li >
201
204
< li > – < a href ="#View-make "> make</ a > </ li >
202
205
< li > – < a href ="#View-handleEvents "> handleEvents</ a > </ li >
203
206
</ ul >
207
+ < a class ="toc_title " href ="#changelog ">
208
+ Change Log
209
+ </ a >
204
210
</ div >
205
211
206
212
< div class ="container ">
@@ -492,10 +498,20 @@ <h2 id="Model">Backbone.Model</h2>
492
498
</ p >
493
499
494
500
< p id ="Model-attributes ">
495
- < b class ="header "> attributes</ b > < code > model.attributes()</ code >
501
+ < b class ="header "> attributes</ b > < code > model.attributes</ code >
502
+ < br />
503
+ The < b > attributes</ b > property is the internal hash containing the model's
504
+ state. Please use < tt > set</ tt > to update the attributes instead of modifying
505
+ them directly. If you'd like to retrieve and munge a copy of the model's
506
+ attributes, use < tt > toJSON</ tt > instead.
507
+ </ p >
508
+
509
+ < p id ="Model-toJSON ">
510
+ < b class ="header "> toJSON</ b > < code > model.toJSON</ code >
496
511
< br />
497
- Return a copy of the model's < b > attributes</ b > . This can be used for persistence,
498
- serialization, or for augmentation before being handed off to a view.
512
+ Return a copy of the model's < b > attributes</ b > for JSON stringification.
513
+ This can be used for persistence, serialization, or for augmentation before
514
+ being handed off to a view.
499
515
</ p >
500
516
501
517
< pre class ="runnable ">
@@ -506,7 +522,7 @@ <h2 id="Model">Backbone.Model</h2>
506
522
507
523
artist.set({birthday: "December 16, 1866"});
508
524
509
- alert(JSON.stringify(artist.attributes() ));
525
+ alert(JSON.stringify(artist));
510
526
</ pre >
511
527
512
528
< p id ="Model-save ">
@@ -519,6 +535,11 @@ <h2 id="Model">Backbone.Model</h2>
519
535
exists on the server, the save will be a < tt > PUT</ tt > . Accepts
520
536
< tt > success</ tt > and < tt > error</ tt > callbacks in the options hash.
521
537
</ p >
538
+
539
+ < p >
540
+ In the following example, notice how because the model has never been
541
+ saved previously, < tt > Backbone.sync</ tt > receives a < tt > "create"</ tt > request.
542
+ </ p >
522
543
523
544
< pre class ="runnable ">
524
545
Backbone.sync = function(method, model) {
@@ -542,11 +563,9 @@ <h2 id="Model">Backbone.Model</h2>
542
563
</ p >
543
564
544
565
< pre >
545
- book.destroy({
546
- success: function(model, response) {
547
- ...
548
- }
549
- });
566
+ book.destroy({success: function(model, response) {
567
+ ...
568
+ }});
550
569
</ pre >
551
570
552
571
< p id ="Model-validate ">
@@ -699,6 +718,15 @@ <h2 id="Collection">Backbone.Collection</h2>
699
718
providing instance < b > properties</ b > , as well as optional properties to be attached
700
719
directly to the collection constructor function.
701
720
</ p >
721
+
722
+ < p id ="Collection-models ">
723
+ < b class ="header "> models</ b > < code > collection.models</ code >
724
+ < br />
725
+ Raw access to the JavaScript array of models inside of the collection. Usually you'll
726
+ want to use < tt > get</ tt > , < tt > at</ tt > , or the < b > Underscore methods</ b >
727
+ to access model objects, but occasionally a direct reference to the array
728
+ is desired.
729
+ </ p >
702
730
703
731
< p id ="Collection-Underscore-Methods ">
704
732
< b class ="header "> Underscore Methods (24)</ b >
@@ -761,14 +789,13 @@ <h2 id="Collection">Backbone.Collection</h2>
761
789
</ p >
762
790
763
791
< pre class ="runnable ">
792
+ var Ship = Backbone.Model;
764
793
var ships = new Backbone.Collection();
765
794
766
795
ships.bind("add", function(ship) {
767
796
alert("Ahoy " + ship.get("name") + "!");
768
797
});
769
798
770
- var Ship = Backbone.Model.extend({});
771
-
772
799
ships.add([
773
800
new Ship({name: "Flying Dutchman"}),
774
801
new Ship({name: "Black Pearl"})
@@ -805,6 +832,37 @@ <h2 id="Collection">Backbone.Collection</h2>
805
832
is sorted, and if your collection isn't sorted, < b > at</ b > will still
806
833
retrieve models in insertion order.
807
834
</ p >
835
+
836
+ < p id ="Collection-comparator ">
837
+ < b class ="header "> comparator</ b > < code > collection.comparator</ code >
838
+ < br />
839
+ By default there is no < b > comparator</ b > function on a collection.
840
+ If you define a comparator function, it will be used to always maintain
841
+ the collection in sorted order. This means that as models are added,
842
+ they are inserted at the correct index in < tt > collection.models</ tt > .
843
+ Comparator functions take a model and return a numeric or string value
844
+ by which the model should be ordered relative to others.
845
+ </ p >
846
+
847
+ < p >
848
+ Note how all of the chapters in this example come out in the
849
+ proper order:
850
+ </ p >
851
+
852
+ < pre class ="runnable ">
853
+ var Chapter = Backbone.Model;
854
+ var chapters = new Backbone.Collection();
855
+
856
+ chapters.comparator = function(chapter) {
857
+ return chapter.get("page");
858
+ };
859
+
860
+ chapters.add(new Chapter({page: 9, title: "The End"}));
861
+ chapters.add(new Chapter({page: 5, title: "The Middle"}));
862
+ chapters.add(new Chapter({page: 1, title: "The Beginning"}));
863
+
864
+ alert(chapters.pluck('title'));
865
+ </ pre >
808
866
809
867
< p id ="Collection-sort ">
810
868
< b class ="header "> sort</ b > < code > collection.sort([options])</ code >
@@ -991,8 +1049,7 @@ <h2 id="View">Backbone.View</h2>
991
1049
< pre >
992
1050
ui.Chapter = Backbone.View.extend({
993
1051
render: function() {
994
- var data = this.model.attributes();
995
- $(this.el).html(this.template.render(data));
1052
+ $(this.el).html(this.template.render(this.model.toJSON()));
996
1053
return this;
997
1054
}
998
1055
});
@@ -1043,8 +1100,7 @@ <h2 id="View">Backbone.View</h2>
1043
1100
},
1044
1101
1045
1102
render: {
1046
- var data = this.document.attributes();
1047
- this.el.html(this.template.render(data));
1103
+ $(this.el).html(this.template.render(this.model.toJSON()));
1048
1104
this.handleEvents();
1049
1105
return this;
1050
1106
}
@@ -1057,7 +1113,7 @@ <h2 id="View">Backbone.View</h2>
1057
1113
1058
1114
1059
1115
1060
- < h2 id ="changes "> Change Log</ h2 >
1116
+ < h2 id ="changelog "> Change Log</ h2 >
1061
1117
1062
1118
< p >
1063
1119
< b class ="header "> 0.1.0</ b > < br />
0 commit comments