Backbone Study Notes: Collection (Collection)

Source: Internet
Author: User
Tags javascript array

Collection (Collection) is a backbone object that organizes and manages multiple models, not just a JavaScript array, but also provides a specialized way to sort, filter, and traverse collections, which can be conveniently communicated with rest servers.

Roomcollection=backbone.collection.extend ({Model:roommodel});

var room1=new roommodel ({name: ' test1 '});

var room2=new roommodel ({name: ' Test2 '});

var room3=new roommodel ({name: ' Test3 '});

var rooms=new roomcollection ([room1,room2,room3]);

Inside the collection, the model is stored in an array named models, and Backbone.collection is responsible for maintaining the array when it adds a model to the collection or deletes the model.

Note : When you sort a collection, the index of the model element changes

The methods provided by the collection:

1. at (): Gets the model at a specific index location, var model=rooms.at (2);

2. IndexOf (): Gets the index of the model, this method inherits from the Underscore.js, Rooms.indexof (model);

3. Clone (): Gets the deep copy of the object, changing the properties of the copied object does not affect the property value of the original model object, rooms.at (2). Clone ();

4. Length: Gets the rooms.length of the set

5. Get (): Use ID to get the model from the collection, guarantee the best performance, if the ID is not initialized, use the object model's CID instead of Id,rooms.get (' C1 ')

6. Add (): Adds a new model Rooms.add (new Roommodel ({name: ' Test4 '}) to the collection, {at:2}), and adds a new model to the collection at index 2.

  The Add method adds only the non-repeating model, you can add a model, or you can add a model array

7. Remove (): Delete one or more models, rooms.remove ([' C1 ', ' C2 '), or rooms.remove (model)

8. Reset (): Empties the model in the collection and can add new models

Use the set as a queue or stack, so push (), pop (), Unshift (), and Shift () methods can all be used

9. Sort (): Sort, by comparator (define collation) property assisted completion

Pluck (): Returns the specific attribute value of each model in the collection as an array, Rooms.pluck (' name ')

Map (): A specific calculation for each model that returns an array of all model calculation results

Rooms.map (function (model) {

Return Model.get (' name ') + ' Hi ';

})

Every (): checks if all the models in the collection meet the specified criteria, and returns false if one does not meet the criteria, returning true if the condition is satisfied

var reslut=rooms.every (function model) {

Return Model.get (' name ') .length>5;

})

Some (): checks whether any model in the collection satisfies the specified condition, returns True if a condition is met, and returns false only if none of the conditions are met

var reslut=rooms.some (function model) {

Return Model.get (' name ') .length>5;

})

Create (): Adds a new model to the collection and saves it to the server, returning the newly added model

var room=rooms.create ({name: ' Test6 '}), the new model added

WHERE (): Returns the array of all models in the collection that match the specified property. is useful for simple filter (filtering) .

Rooms.where ({name: ' Test1 '})

Findwhere (): Returns the first model in a collection that matches the specified model

var room=rooms.findwhere ({name: ' Test1 '})

Backbone Study Notes: Collection (Collection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.