Javatpoint Logo
Javatpoint Logo

Backbone.js Collection

A collection is an ordered set of models.

It is used to deal with a group of related models.

It handles the loading and saving of new models to the server.

It provides helper functions to perform aggregation and computation against a list of models.

You can create your own collection by extending the backbone's collection class.

If an event is triggered on a model in a collection then this will also be triggered on the collection directly. It facilitates you to listen for changes to specific attributes in any model in a collection.

Following is a list of methods that can be used to manipulate the Backbone.js collection:

Index Method Description
1. extend It is used to extend the backbone's collection class to create an own collection.
2. model It is used to specify the model class. You need to override the model property of the collection class.
3. initialize Initialize function is defined to create a model instance.
4. models It specifies the array of models which are created inside of the collection.
5. toJSON It returns the copy of the attributes of a model using JSON format in the collection.
6. sync It specifies the state of the model and uses backbone.sync to display the state of the collection.
7. add It is used to add a model or array of models to the collection.
8. remove It removes a model or array of models from the collection.
9. reset It resets the collection and populates with new array of models or empty the entire collection.
10. set It is used to update the collection with set of items in a model. If any new model is found, the items will be added to that model.
11. get It is used to retrieve the model from a collection by using id or Cid.
12. at It is used to retrieve the model form a collection by using specified index.
13. push It is similar to add() method which takes array of models and push the models to the collection.
14. pop It is similar to remove() method which takes array of models and remove the models from the collection.
15. unshift It is used to add specified model at the beginning of a collection.
16. shift It is used to remove the first item from the collection.
17. slice It is used to display the shallow copy of the elements from the collection model.
18. length It is used to count the number of models in the collection.
19. comparator It sorts the items in the collection.
20. sort It is used to sort the items in the collection. it also uses comparator property in order to sort the items.
21. pluck It is used to retrieve the attributes from the model in the collection.
22. where It is used to display the model by using the matched attribute in the collection.
23. findWhere It is used to return the model that matches the specified attribute in the collection.
24. url It creates an instance of the collection and returns where resource is located.
25. parse It returns the collection's data by passing through the response object and represents the data in JSON format.
26. clone It is used to return the shallow copy of the specified object.
27. fetch It uses the sync method to extract data from the model in the collection.
28. create It creates new instance of the model in the collection.

Underscore Methods:

The following table specifies underscore.js methods which provide their functionality to be used on the Backbone.Collection.

Index Method Description
1. _.each(list, iteratee, [context]) It iterates each of the elements in the collection using iteratee function.
2. _.map(list, iteratee, [context]) It maps each value and displays them in a new array of values using iteratee function.
3. _.reduce(list, iteratee, memo, [context]) It is used to reduce list of values into single value. It is also known as inject and foldl.
4. _.reduceright(list, iteratee, memo, [context]) It is right associative version of reduce.
5. _.find(list, predicate, [context]) It finds each value and returns the first one which passes the predicate or test.
6. _.filter(list, predicate, [context]) It filters each value and returns the array of values which passes the predicate or test.
7. _.reject(list, predicate, [context]) It returns the rejected elements in the list which doesn't pass the predicted values.
8. _.every(list, predicate, [context]) It returns true, if elements in the list pass the predicted values.
9. _.some(list, predicate, [context]) It returns true, if elements in the list pass the predicted values.
10. _.contains(list, value, [fromindex]) It returns true, if value present in the list.
11. _.invoke(list, methodname, *arguments) It invokes the method name using method name() on each value in the list.
12. _.max(list, [iteratee], [context]) It specifies the maximum value in the list.
13. _.min(list, [iteratee], [context]) It specifies the minimum value in the list.
14. _.sortby(list, [iteratee], [context]) It returns the sorted elements in ascending order by using iteratee in the list.
15. _.groupby(list, [iteratee], [context]) It divides the collection values into sets, grouped by using iteratee in the list.
16. _.shuffle(list) It returns shuffled copy of the list.
17. _.toarray(list) It defines an array of the list.
18. _.size(list) It defines the number of values in the list.
19. _.first(array, [n]) It specifies the first element of the array in the list.
20. _.initial(array, [n]) It returns everything, but specifies the last entry of the array in the list.
21. _.last(array, [n]) It specifies the last element of the array in the list.
22. _.rest(array, [index]) It defines rest of the elements in the array.
23. _.without(array, *values) It returns values of all instances which are removed in the list.
24. _.indexof(array, value, [issorted]) It returns value if it found at specified index or returns -1, if it is not found.
25. _.indexof(array, value, [fromindex]) It returns last occurrence of the value in the array or returns -1, if it is not found.
26. _.isempty(object) It returns true if there are no values in the list.
27. _.chain(obj) It is used to return a wrapped object.




Please Share

facebook twitter google plus pinterest tumbler

Latest 4 Tutorials