Backbone.js: Collection Cheat Sheet

MethodsSyntaxDescription
extendBackbone.Collection.extend(properties, [classProperties])Extends the backbone’s collection class to create a collection.
modelcollection.model([attrs], [options])To specify the model class, we need to override the model property of the collection class.
modelIdcollection.modelId(attrs)Override this method to return the value the collection will use to identify a model given its attributes.
preinitializenew Backbone.Collection([models], [options])For use with collections as ES classes. If you define a preinitialize method, it will be invoked when the Collection is first created and before any instantiation logic is run for the Collection.
constructor / initializenew Backbone.Collection([models], [options])When a model instance is created, it is invoked by defining the initialize function when the collection is created.
modelscollection.modelsRaw access to the JavaScript array of models inside of the collection.
toJSONcollection.toJSON([options])Return an array containing the attributes hash of each model in the collection.
synccollection.sync(method, collection, [options])It represents the state of the model and uses the Backbone.sync to display the state of the collection.
addcollection.add(models, [options])Add a model (or an array of models) to the collection, firing an “add”
 event for each model, and an “update”
 event afterwards.
removecollection.remove(models, [options])Remove a model (or an array of models) from the collection, and return them.
resetcollection.reset([models], [options])Adding and removing models one at a time is all well and good, but sometimes you have so many models to change that you’d rather just update the collection in bulk.
setollection.set(models, [options])The set method performs a “smart” update of the collection with the passed list of models. 
getcollection.get(id)It is used to retrieve the model from a collection by using the idor cid.
atcollection.at(index)Retrieve the model from a collection by using specified index.
pushcollection.push(model, [options])Add a model at the end of a collection. Takes the same options as add.
popcollection.pop([options])Remove and return the last model from a collection. Takes the same options as remove.
unshiftcollection.unshift(model, [options])Add a model at the beginning of a collection. Takes the same options as add.
shiftcollection.shift([options])Remove and return the first model from a collection. Takes the same options as remove.
slicecollection.slice(begin, end)Return a shallow copy of this collection’s models, using the same options as native Array#slice.
lengthcollection.lengthLike an array, a Collection maintains a length
 property, counting the number of models it contains.
comparatorcollection.comparatorIt is used to sort the items in the collection.
sortcollection.sort([options])Sorts the items in the collection and uses comparator property in order to sort the items.
pluckcollection.pluck(attribute)Pluck an attribute from each model in the collection. Equivalent to calling map
 and returning a single attribute from the iterator.
wherecollection.where(attributes)Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of filter
findWherecollection.findWhere(attributes)It returns the model, that matches the specified attribute in the collection.
urlcollection.url or collection.url()Set the url property (or function) on a collection to reference its location on the server.
parsecollection.parse(response, options)Returns the collection’s data by passing through the response object and represents the data in JSON format.
clonecollection.clone()Returns a new instance of the collection with an identical list of models.
fetchcollection.fetch([options])Fetch the default set of models for this collection from the server, setting them on the collection when they arrive. 
createcollection.create(attributes, [options])Convenience to create a new instance of a model within a collection. 
mixinBackbone.Collection.mixin(properties)mixin provides a way to enhance the base Backbone.Collection and any collections which extend it. 

Leave a Reply

Your email address will not be published. Required fields are marked *