A Backbone.js Collections are a group of related models. It is useful when the model is loading to the server or saving to the server. Collections also provide a helper function for performing computation against a list of models. Aside from their own events collections also proxy through all of the events that occur to the model within them, allowing you to listen in one place for any change that might happen to any model in the collection.
Now we will see the working of collections.
Syntax: We can create a collection with extending syntax as follows
let collection = Backbone.Collection.extend ( Properties , [ ClassProperties ] );
- Properties: These are the properties that are assigned to the instance of collection when initiated.
- Class-Properties: These are the properties that are attached to the collection’s constructor function.
Example 1: In this example, we will create a collection and pass the argument as a list of models creating the array. The following code shows how we can add a list of models at the creation of the Collection.
Example 2: In this example, we will create a collection and add the model to the collection with the add method.