[From: backbone.js Development of the 3rd chapter]
Collection API
(function ($) { //Define----------------------------- varModeld =Backbone.Model.extend ({defaults: {ID:0, Name:‘‘}, Idattribute:' ID ' }); //Defining Collection Objects /*inside, the model is stored in an array named models.*/ varCollectiond =Backbone.Collection.extend ({Model:modeld}); //Instance--------------------------- varModelinstance =NewModeld ({ID:1, Name:' James ' }); //instantiating a Collection object varCollectioninstance =NewCollectiond ([{ID:2, Name: ' Yoyo '}, modelinstance]); //Apply----------------------------- //gets the collection item at the index varModel2 = collectioninstance.at (1); //Get model Index values varModel2index =Collectioninstance.indexof (modelinstance); //Get Collection Length varCount =collectioninstance.length; //gets the model for the specified ID /*querying a collection by its internal _byid key value*/ varModel1 = Collectioninstance.get (2); //add model [repetition is not added to the collection, triggering the Add event]Collectioninstance.add ([{ID:3, Name:' Pepe '}], {at:0//insertion point }); //Deleting a modelCollectioninstance.remove ([3]); //Resetting the modelCollectioninstance.reset ([{ID:4, Name:' Ramos ' }]); //operate as a queue or as a stack /*. push (). Pop (). Unshift (). Shift ()*/ //Sort /*Note: After defining the comparator callback function, the insertion value is called to ensure the order*/Collectioninstance.comparator=function(model) {returnModel.get (' Name '); }; //or /*Collectioninstance.comparator = function (m1, m2) {return m1.get (' id ')-m2.get (' id '); }; */ /*after the model is updated, you need to manually call sort () to sort or bind to the Change event*/Collectioninstance.sort (); //Filter Collection varCollection2 = Collectioninstance.where ({Name: ' Pepe ') }); //iterating through the collectionCollectioninstance.each (function(mode, index, list) {}); //whether the conditions are met varIsmultiple = Collectioninstance.every (function(model) {returnModel.get (' ID ') > 0; }); //whether the condition is met arbitrarily varIsother = Collectioninstance.some (function(model) {returnMode.get (' ID ') = = = 0; }); //gets the collection of values for the specified property of the set varnames = Collectioninstance.pluck (' Name '); //gets a collection-specific collection of values varPluscoll = Collectioninstance.map (function(model) {returnModel.get (' ID ') + model.get (' Name ')); }); //Get merge Single value varPlusid = Collectioninstance.reduce (function(Plusvalue, model) {return(Plusvalue | | 0) + model.get (' ID ')); }, 0/*Initial value*/); //chained operation for collection/* CHAIN () */ varplusID2 = Collectioninstance.chain (). Pluck (' ID '). Reduce (function(Plusvalue, value) {returnPlusvalue +value; }, 0). Value ();}) (jQuery);
Plugin extensions:
1. NoSQL Query More API
(function ($) { //Dependent backbone.query.js //Https://github.com/davidgtonge/backbone_query //Define-------------------------------------- varMODELQ =Backbone.Model.extend ({defaults: {ID:0, Name:‘‘, Age:0, rank:1001 } }); //plug-in extension through its base class varCollectionq =Backbone.QueryCollection.extend ({MODEL:MODELQ}); //instance---------------------------------------- varCollectioninstance =NewCollectionq ([{ID:1, Name: ' Cxls ', age:33, rank:1999}, {ID:2, Name: ' Yln ', age:22, rank:1003}, {ID:3, Name: ' PP ', age:31, rank:1899}, {ID:1, Name: ' LMS ', age:28, rank:1899 } ]); //Apply-------------------------------------------- //Enquiry varQcollection = Collectioninstance.query ({name: ' Cxls ') }); /*operator*/ /*$equal = = = $ne!== $in contains the values of all possible arrays [1899,1999] $nin! $in $exists Whether there is true or flase $has exists true or flase $and $or $nor! $or $not! $and */qcollection= Collectioninstance.query ({rank: {$equal: 1299 } }); Qcollection= Collectioninstance.query ({$and: {age:31, rank:1299 } }); //Sort varOcollection = Collectioninstance.query ({sortby: ' name ', Order: ' desc ')/*default ASC*/ }); //page Out varPcollection = Collectioninstance.query ({rank:1999}, {limit:10/*returns the first N elements of a result array (required)*/, offset:1/*Skip Top N result items*/, page:2,/*Specify the results page*/Cache:true/*whether to cache data by default false*/ }); /*Note: If there is an update after the cache, you can manually synchronize the cache by calling the Resetquerycache () method*/}) (jQuery);
View Code
2. Collections store different types of models
(function ($) { //Dependent backbone.chosen.js //Https://github.com/asciidisco/backbone.Chosen //Define-------------------------------------- varModel1 =Backbone.Model.extend ({getfullname:function () { return This. Get (' firstName ') + This. Get (' LastName '); } }); varModel2 =Backbone.Model.extend ({getfullname:function () { return This. Get (' name1 ') + This. Get (' name2 '); } }); /*Backbone.chosen overrides the _preparemodel method to select the correct model object based on the map (map)*/ varCollectionmultiple =Backbone.Collection.extend ({model: {chosen: {//define multiple types [You can also use a function to map a model]attr: ' type ',//Type property names in the modelDefaults:model1,//Default TypeMap: {//type name Correspondence modelModel1:model1, Model2:model2}} } }); //instance------------------------ varCollectionm =NewCollectionmultiple ([{firstName:' Yo ', lastName: ' Yo ', type: ' Model1 '}, {name1:' C ', name2: ' Yo ', Ronaldo: ' Model2 ' } ]);}) (jQuery);
View Code
3. A one-to-many association relationship
(function ($) { //Dependent backbone.relational.js //https://github.com/pauluithol/backbone-relational //define---------------------------------------------- varEmployeemodel =Backbone.RelationalModel.extend (); varEmployeecollection =Backbone.Collection.extend ({model:employeemodel}); varSupervisermodel =Backbone.RelationalModel.extend ({relations: [{type:Backbone.HasMany,//One -to-many-type enumerationKey: ' Employees ', Relatedmodel:employeemodel, Collectiontype:employeecollection,//collection typereverrelation: {key:' Superviser '}, Includeinjson: [' id ', ' name ']//controls export to JSON-formatted properties }] }); //instance--------------------------------------- varSuperviserinstance =NewSupervisermodel ({ID:1, Name:' Yoyo ', Employees: [{ID:2, Name: ' ABC '}, {ID:3, Name: ' EDF ' } ] }); //Apply------------------------------------------ varEmployee1name = Superviserinstance.get (' employees '). at (0). Get (' name '); varSupname = Superviserinstance.get (' employees '). at (0). Get (' Superviser '). Get (' name '); //add an object or relationship [auto-correlate one-to-many relationships]Superviserinstance.get (' Employees '). Add ({ID:5, Name:' XYZ ' }); varNewEmployee =NewEmployeemodel ({ID:4, Name:' Ghi ', superviser:superviserinstance}); //get JSON Form Object varSuperjson =Superviserinstance.tojson ();}) (jQuery);
View Code