One: Set concept
A collection is multiple models, and if the model is understood as a row in a table structure, then the set collection is a table, consisting of multiple rows. We often need to use collections to organize and manage multiple models.
Two: Create a collection
1: Extend the Backbone.collection object, specify which model to create the collection for, and then pass the parameter model.
var _collection = Backbone.Collection.extend ({
Model: _model
});
2: Create set instance, pass model array parameter to initialize; It is assumed that the Dafault property of _model is (name,gender,age) and the instance object is Testmodel
var testobj = new _collection (
[{name: ' 001 ', Gender: ' Women ', Age: ' + '}, {name: ' 002 ', Gender: ' Man ', Age: ' "}, {name: ' 003 ', Gender: ' Women ', Age: ' 28 '}]);
Inside the collection, the model is stored in an array named models
In addition to the array parameters to initialize, you can also initialize with a single model object .
such as: Model1 = new _model ({name: ' 001 ', Gender: ' Women ', Age: ' 23 '}); Model2 = new _model ({name: ' 002 ', Gender: ' Man ', Age: ' 33 '});
var testobj = new _collection ([Model1, Model2]);
Three: Operations on a collection
1:at () Gets a model in the collection by index
var model = testobj.at (2); {name: ' 003 ', Gender: ' Women ', Age: ' 28 '}
Model.get (' name '); 003
inside the collection, the model is stored in an array named Models , whose first element index is labeled 0
2:indexof () Gets the index value of the model this method inherits from the Underscore.js.
As the model above, Testobj.indexof (model); 2
3:clone () Gets a deep copy of a model object.
(Deep copy: Use the Clone method so that the cloned model object is not the same object as the object that is actually stored in the collection, and one value change does not affect other objects)
(Shallow copy: The model object obtained with the at () index value is the same object as the object in the collection, which affects the interaction)
such as: another = testobj.at (2). Clone (); Another.set (' name ': ' Test ');
Another.get (' name '); Test testobj.at (2). Get (' name '); 002
3:length () Gets the set length len = testobj.length//3 model = testobj.at (len-1)
4:add () Add a new model to the end of the collection
Testobj.add ({name: ' 004 ', Age: ' + ', Denger: ' Women '}) plus a model,
Testobj.add ([{name: ' 005 ', Age: ' + ', Denger: ' Women '},{name: ' 006 ', Age: ' + ', Denger: ' Women '}]) plus model array
You can also add existing model objects
Note: The Add method ensures that duplicate models are not added to the collection, and are not duplicated in the internal array models
5:{at:index} Add a model at the specified location
Testobj.add ({name: ' $ ', Age: ' + ', Denger: ' Women '}, {at:0}) at this time testobj.at (0). Get (' name ') output 000
6:remove () removes the model, which can be either a single value or an array, which can be passed into the model's id,cid, or the model object
such as Testobj.remove (model) Testobj.remove ([C1,C2])
When you call remove, the model is removed from models and the reference relationship between the model and the collection is deleted
7:reset () Delete/empty All models in the collection and add new models
such as: Testobj.reset ([{name: ' A ', Age:3,gender: ' Boy '},{name: ' B ', Age:2,:gender: ' Gril '}]);
8: Use set push pop unshift shift in stacks and queues
Testobj.push (model)
9: Sort comparator The callback function takes a model object as its unique parameter, returning the property to be sorted.
A:testobj.comparator = function (Testmodel) {return testmodel.get (' age ');} Collections are sorted according to age
B:testobj.sort (); Call Sort Manual Departure sort
When the comparator callback function is called, the callback is called whenever a new model is inserted, inserting the new model into the appropriate location.
10:where () filters the model in the collection to pass in one or more search criteria
such as: Var result= testobj.where ({name: ' A '}); At this point, result is only an array of models of the internal model array, and to use it, a new object array
var resultcollection = new _collection (result)//{name: ' A ', Age:3,gender: ' Boy '}
11: Traverse Collection each ()
str = ";
Testobj.each (function (model,index,list) {
str + = model.get (' name ');} Ab
Parameters: Model: Current models; index: Subscript of the current model in the collection; list: Entire model array
12:every () detection meets all conditions
person = Testobj.every (the function (model) {return model.get (' age ') >18;}) False to return False if one does not meet the condition
13:some () returns True if a condition is met
14:pluck () returns an array of property values in the collection such as: Testobj.pluck (' name ')//[' A ', ' B ']
15:map () calculates the values of each model property, returns the array Testobj.map (model) {return model.get (' name ') + ' +model.get (' Gender ')})//[' A Boy ', ' B Gril ']
16:reduce () merges a model into a single value testobj.reduce (function (Memo,model) {return memo+model.get (' age ')},0)//5
17:chain () chain operation, which can pass the result of a method after it has been called. To invoke another method.
Suppose collection's models is [{num:22,count:10,name: ' AA '},{num:1,count:2,name: ' Test '}]
Testobj. chain (). Map (function (model) {
return model.get (' num ') +model.get (' count '); [' 32 ', ' 3 ']
}). Reduce (function (memo,val) {
Return memo + val;}). Value (); 35