Details on the collection of Backbone. js (below)
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
Iv. constructor and initialization
When we create a set, we can pass the initialization array of the model. The comparator of the set can be added as an option. If the passed comparator option is false, sorting is blocked. If we define an initialization function, this function will be called when the set is created. The following describes several options. If they are provided, they are directly added to the set: Model and comparator.
var tabs = new TabSet([tab1, tab2, tab3]);var spaces = new Backbone.Collection([], { model: Space});
V. toJSON
The toJSO method returns an array that contains the hash attributes of each model. This method is usually used for serialization and persistence of the entire set.
var song = new Backbone.Collection([ {name: "Flatts"}, {name: "OMC"}, {name: "Jackson"}]);alert(JSON.stringify(song));
6. Comparator
By default, the set does not contain a comparator. If we define a comparator, it can be used to maintain a certain sort of the set. This means that when a model is added, the model will be inserted to a suitable position in the set. The comparator can be defined by sortBy or indicated by a string.
The sortBy comparator function returns a model and a number or string.
The sort comparator function obtains two models. If the first model prevails over the second model,-1 is returned. If the two models have the same level, 0 is returned. If the second model prevails over the first model, then 1 is returned.
Let's take a look at the example below:
var student = Backbone.Model;var students = new Backbone.Collection;students.comparator = 'name';students.add(new student({name: "name1", roll: 9}));students.add(new student({name: "name2", roll: 5}));students.add(new student({name: "name3", roll: 1}));alert(students.pluck('roll'));
The set to be compared will not be automatically reordered, even if we modify the attributes of the model. Therefore, we should call the sort method when modifying the model attribute to estimate whether the sorting will be affected.
VII. Sorting
When adding models to a set, the set should be forcibly re-ordered. To disable sorting when adding models to a set, you can pass the {sort: false} parameter. Call the sort trigger to check this parameter.
sortByType: function(type) { this.sortKey = type; this.sort();}
And view functions:
sortThingsByColumn: function(event) { var type = event.currentTarget.classList[0] this.collections.things.sortByType(type) this.render()}
8. Picking
Pluck: extracts an attribute from each model in the set. This is equivalent to calling Map from the iterator and returning a single attribute.
var song = new Backbone.Collection([ {name: "Flatts"}, {name: "OMC"}, {name: "Jackson"}]);var names = songs.pluck("name");alert(JSON.stringify(names));
9. Where
Where: returns an array of all models that match the passed attributes in the set. A filter is used.
var song = new Backbone.Collection([ {name: "Yes I Do", artist: "Flatts"}, {name: "How Bizarre", artist: "How Bizarre"}, {name: "What Hurts the Most", artist: "Flatts"}, ]);var artists = song.where({artist: "Flatts"});alert(artists.length);
10. URL
Set the URL attribute in the collection to reference the location of the server. The model in the Set uses this URL to construct its own URL.
var Songs = Backbone.Collection.extend({ url: '/songs'});var Songs = Backbone.Collection.extend({ url: function() { return this.document.url() + '/songs'; }});
XI. Analysis
Parse: This is called by Backbone when fetch is extracted, regardless of whether the server returns the set model. This function will pass the original response object, which should return an array of model attributes added to the set. By default, the no-op operation is null. Simply pass through the JSON response, overwrite the operation with the existing API, or better respond to the namespace.
var songs = Backbone.Collection.extend({ parse: function(response) { return response.results; }});
12. Extraction
Fetch: extracts the default model set of the set from the server. After retrieval, set them in the set. This option is hashed by success or error callback. It passes three parameters (set, response, and option. Then the model data is returned from the server. It is used to set the model for merging and extraction.
Backbone.sync = function(method, model) { alert(method + ": " + model.url);};var songs = new Backbone.Collection;songs.url = '/songs';songs.fetch();
As we can see above, only the collection of Backbone has so many methods that mastering them can improve the quality of code.