BackboneJS - Collection


Advertisements


Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly. This allows you to listen for changes to specific attributes in any model in a collection.

The following table lists down all the methods which you can use to manipulate the BackboneJS-Collection −

S.No. Methods & Description
1 extend

Extends the backbone's collection class to create a collection.

2 model

To specify the model class, we need to override the model property of the collection class.

3 initialize

When a model instance is created, it is invoked by defining the initialize function when the collection is created.

4 models

Array of models which are created inside the collection.

5 toJSON

Returns the copy of the attributes of a model using the JSON format in the collection.

6 sync

It represents the state of the model and uses the Backbone.sync to display the state of the collection.

7 add

Add a model or array of models to the collection.

8 remove

Removes a model or array of models from the collection.

9 reset

It resets the collection and populates with new array of models or will empty the entire collection.

10 set

It is used to update the collection with a 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 the idor cid.

12 at

Retrieve the model from a collection by using specified index.

13 push

It is similar to the add() method which takes the array of models and pushes the models to the collection.

14 pop

It is similar to the remove() method which takes the array of models and removes the models from the collection.

15 unshift

Add a specified model at the beginning of a collection.

16 shift

It removes the first item from the collection.

17 slice

Displays the shallow copy of the elements from the collection model.

18 length

Counts the number of models in the collection.

19 comparator

It is used to sort the items in the collection.

20 sort

Sorts the items in the collection and uses comparator property in order to sort the items.

21 pluck

Retrieves 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 returns the model, that matches the specified attribute in the collection.

24 url

It creates an instance of the collection and returns where resources are located.

25 parse

Returns the collection's data by passing through the response object and represents the data in JSON format.

26 clone

It returns the shallow copy of the specified object.

27 fetch

It extracts the data from the model in the collection using the sync method.

28 create

It creates a new instance of the model in the collection.

Underscore Methods

The following table lists down the Underscore.js methods which provides their functionality to be used on the Backbone.Collection.

S.No. Methods & Description
1

_.each(list, iteratee, [context])

Iterates each of the elements in the collection using the iteratee function.

2

_.map(list, iteratee, [context])

It maps each value and displays them in a new array of values using the iteratee function.

3

_.reduce(list, iteratee, memo, [context])

It reduces the list of values into a single value and it also known as inject and foldl.

4

_.reduceRight(list, iteratee, memo, [context])

It is the 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 do not 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 a value is present in the list.

11

_.invoke(list, methodName, *arguments)

It invokes the method name using methodName() 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 the ascending order by using iteratee in the list.

15

_.groupBy(list, [iteratee], [context])

It divides the collection values into the sets, grouped by using the iteratee in the list.

16

_.shuffle(list)

It returns the 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 the remaining elements in the array.

23

_.without(array, *values)

It returns the values of all instances which are removed in the list.

24

_.indexOf(array, value, [isSorted])

It returns the value if it is found at a specified index or returns -1, if it is not found.

25

_.indexOf(array, value, [fromIndex])

It returns the 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 returns a wrapped object.



Advertisements
E-Books Store