Wednesday, June 17, 2026
HomeLanguagesJavascriptBackbone.js add Collection

Backbone.js add Collection

The Backbone.js add collection is utilized to include a model or an array of models to the given collection. It takes the model as the first parameter and options as the second parameter. In this article, we will discuss the Backbone.js add collection.

Syntax:

collection.add(models, [options])   

Parameter Values: It accepts 2 parameters, that are described below:

  • models: This parameter is used to specify the names of the instances that are added to the collection.
  • options: This parameter takes the model type which will be added to the given collection.

Example 1: In this example, we are adding 2 items to a collection array.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Backbone.js add Collection</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
 
<body>
    <script type="text/javascript">
       
        // 'Food' is a model and that contains the
        // default value for the  model 
        var Food = Backbone.Model.extend({
            defaults: {
                food_region: "India"
            }
        });
       
        // Here the  'FoodCollection' is a collection instance and
        // model 'Food' is specified by overriding the 'model' property 
        var FoodCollection = Backbone.Collection.extend({
            model: Food
        });
       
        // The instances "food1" and "food2" are created
        // for the model "Food" 
        var food1 = new Food({
            name: "Icecream",
            country: "Hyderabad"
        });
        var food2 = new Food({
            name: "cake/chocos",
            country: "Guntur"
        });
       
        // The add() method adds the models 'food1' and 'food2'
        // to the collection instance 'final' 
        var final = new FoodCollection();
        final.add([food1, food2]);
       
        // Display the items in the collection
        document.write("Values :", JSON.stringify(final.toJSON()));
        document.write("<br>");
    </script>
</body>
</html>


Output:

Values :
[{"name":"Icecream","country":"Hyderabad","food_region":"India"},
{"name":"cake/chocos","country":"Guntur","food_region":"India"}]

Example 2: In this example, we will add 2 items again 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Backbone.js add Collection</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
 
<body>
    <script type="text/javascript">
     
        // 'Food' is a model and that contains the
        // default value for the  model 
        var Food = Backbone.Model.extend({
            defaults: {
                food_region: "India"
            }
        });
         
        // Here the  'FoodCollection' is a collection instance and model
        // 'Food' is specified by overriding the 'model' property 
        var FoodCollection = Backbone.Collection.extend({
            model: Food
        });
         
        // The instances "food1" and "food2" are created for
        // the model "Food" 
        var food1 = new Food({
            name: "Icecream",
            country: "Hyderabad"
        });
        var food2 = new Food({
            name: "cake/chocos",
            country: "Guntur"
        });
         
        // The add() method adds the models 'food1' and
        // 'food2' to the collection instance 'final' 
        var final = new FoodCollection();
        final.add([food1, food2]);
         
        // Display the items in the collection
        document.write("Values : ", JSON.stringify(final.toJSON()));
        document.write("<br>");
        document.write("<br>");
        var food3 = new Food({
            name: "fish",
            country: "delhi"
        });
        var food4 = new Food({
            name: "onions",
            country: "indore"
        });
        final.add([food3, food4]);
        document.write("Values again added: ",
            JSON.stringify(final.toJSON()));
    </script>
</body>
</html>


Output:

Values : 
[{"name":"Icecream","country":"Hyderabad","food_region":"India"},
{"name":"cake/chocos","country":"Guntur","food_region":"India"}]

Values again added: 
[{"name":"Icecream","country":"Hyderabad","food_region":"India"},
{"name":"cake/chocos","country":"Guntur","food_region":"India"},
{"name":"fish","country":"delhi","food_region":"India"},
{"name":"onions","country":"indore","food_region":"India"}]

Reference: https://backbonejs.org/#Collection-add

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS