Saturday, October 25, 2025
HomeLanguagesJavascriptBackbone.js at Collection

Backbone.js at Collection

In this article, we will discuss Backbone.js at collection. The Backbone.js at collection is used to return the model from the given collection by specifying the index. Indexing starts with 0.

Syntax:

collection.at(models,options)   

Parameters: It will take two parameters.

  • models: this is the first parameter that is used to specify the names of the instances to be pushed into the collection, 
  • options: this parameter takes the model type which will be added to the specified collection  {at:index} .

Example 1: In this example, we will return the first index values.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
        type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
 
    <script type="text/javascript">
     
        // 'Food' is a model and that contains the 
        // default value for the  model 
        var Food = Backbone.Model.extend({
            defaults: {
                food_name: "Butter",
                food_region: "Hyderabad"
            }
        });
 
        // 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"
        });
         
        var food3 = new Food({
            name: "Icecream",
            country: "Hyderabad"
        });
 
        var final = new FoodCollection();
        final.add([food1, food2, food3]);
 
        var food4 = new Food({
            name: "cake/chocos",
            country: "Guntur"
        });
         
        final.add(food4, { at: 0 });
 
        document.write('New Values: ' +
            JSON.stringify(final.toJSON()));
    </script>
</head>
 
<body></body>
 
</html>


Output:

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

Example 2: In this example, we will return the last index values.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
            type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script type="text/javascript"> 
         // 'Food' is a model and that contains the 
         // default value for the  model 
         var Food = Backbone.Model.extend({ 
            defaults: { 
               food_name: "Butter", 
               food_region:"Hyderabad" 
            } 
         }); 
   
         // 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"});
         var food3 = new Food({name: "Icecream",
                               country:"Hyderabad"}); 
            
         var final = new FoodCollection(); 
         final.add([food1,food2,food3]); 
    
         var food4 = new Food({name: "cake/chocos", 
                               country:"Guntur"});
         final.add(food4,{at:3}); 
   
         document.write( 'New Values: ' +
                        JSON.stringify(final.toJSON()));
      </script> 
</head>
 
<body></body>
 
</html>


Output:

New Values: [{
    "name":"Icecream",
    "country":"Hyderabad",
    "food_name":"Butter",
    "food_region":"Hyderabad"
},
{
    "name":"cake/chocos",
    "country":"Guntur",
    "food_name":"Butter",
    "food_region":"Hyderabad"
},
{
    "name":"Icecream",
    "country":"Hyderabad",
    "food_name":"Butter",
    "food_region":"Hyderabad"
},
{
    "name":"cake/chocos",
    "country":"Guntur",
    "food_name":"Butter",
    "food_region":"Hyderabad"
}]
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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS