Backbone.js Collection

 

A set of models is called a collection. The type of model and the instance of properties can be defined for a collection. The loading and saving of new models to the server is handled by a collection along with the feature of providing helper functions aggregation and computation. A collection is created to serve the purpose of event binding when a modification is made in the collection. The data from the database is retrieved and is populated by the model and collection by binding custom events. The list of models in collection support sorting and filtering and thus the processing in the loop is easier. All the Backbone.JS Collection’ methods are listed below:

 

METHOD USES
extend() To extend the backbone’s collection class to create own collection.
model() To specify the model class.
initialize() To create a model instance.
models() To specify the array of models which are created inside of the collection.
toJSON() To return the copy of the attributes of a model using JSON format in the collection.
sync() To specify the state of the model and to display the state of the collection.
add() To add a model or array of models to the collection.
remove() To remove a model or array of models from the collection.
reset() To reset the collection and populate it with a new array of models or empty the entire collection.
set() To update the collection with a set of items in a model.
get() To retrieve the model from a collection by using id or Cid.
at() To retrieve the model form a collection by using a specified index.
push() To push the models to the collection.
pop() To remove the models from the collection.
unshift() To add a specified model at the beginning of a collection.
shift() To remove the first item from the collection.
slice() To display the shallow copy of the elements from the collection model.
length() To count the number of models in the collection.
comparator() To sort the items in the collection.
sort() To sort the items in the collection.
pluck() To retrieve the attributes from the model in the collection.
where() To display the model by using the matched attribute in the collection.
findWhere() To return the model that matches the specified attribute in the collection.
URL() To create an instance of the collection and returns where the resource is located.
parse() To get the collection’s data by passing through the response object.
clone() To get the shallow copy of the specified object.
fetch() To extract data from the model in the collection.
create() To create a new instance of the model in the collection.

 

Underscore Methods:

Listed below are the underscore.js methods used for the Backbone.Js collection.

 

METHOD USES
_.each(list, iteratee, [context]) To iterate each of the elements in the collection using iteratee function.
_.map(list, iteratee, [context]) To map each value and display them in a new array of values using iteratee function.
_.reduce(list, iteratee, memo, [context]) To reduce list of values into a single value.
_.reduceright(list, iteratee, memo, [context]) To act as a right associative version of reduce.
_.find(list, predicate, [context]) To find each value and to return the first one which passes the predicate or test.
_.filter(list, predicate, [context]) To filter each value and to return the array of values which passes the predicate or test.
_.reject(list, predicate, [context]) To return the rejected elements in the list which doesn’t pass the predicted values.
_.every(list, predicate, [context]) To return true, if the elements in the list passes the predicted values.
_.some(list, predicate, [context]) To return true, if the elements in the list passes the predicted values.
_.contains(list, value, [fromindex]) To return true, if value is present in the list.
_.invoke(list, methodname, *arguments) To invoke the method name using method name() on each value in the list.
_.max(list, [Toeratee], [context]) To specify the maximum value in the list.
_.min(list, [Toeratee], [context]) To specify the minimum value in the list.
_.sortby(list, [Toeratee], [context]) To return the sorted elements in ascending order by using iteratee in the list.
_.groupby(list, [Toeratee], [context]) To divide the collection values into sets, grouped by using iteratee in the list.
_.shuffle(list) To return shuffled copy of the list.
_.toarray(list) To define an array of the list.
_.size(list) To define the number of values in the list.
_.first(array, [n]) To specify the first element of the array in the list.
_.initial(array, [n]) To return everything, but specifies the initial entry of the array in the list.
_.last(array, [n]) To specify the last element of the array in the list.
_.rest(array, [index]) To define the rest of the elements in the array.
_.without(array, *values) To return values of all instances which are removed in the list.
_.indexof(array, value, [issorted]) To return value if it is found at specified index or returns -1, if it is not found.
_.indexof(array, value, [fromindex]) To return last occurrence of the value in the array or returns -1, if it is not found.
_.isempty(object) To return true if there are no values in the list.
_.chain(obj) To return a wrapped object.