Monday, November 18, 2024
Google search engine
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>
        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>
            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!

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments