Thursday, August 28, 2025
HomeLanguagesJavascriptBackbone.js get Collection

Backbone.js get Collection

The Backbone.js get Collection is used  to retrieve model from a Collection. This method uses unique identified to get the model we can use user define id value or by default cid value or model name. 

Syntax: 

collection.get ( id );

Parameters: 

  • id: It is a unique identifier that is used to identify the model in the collection.

Example 1: In this example, we will illustrate the Backbone.js get Collection. We will use default cid as a unique identifier in this example.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>BackboneJS get collection</title>
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
 
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>BackboneJS get collection</h3>
     
    <script type="text/javascript">
        var Book = Backbone.Model.extend();
 
        var books = Backbone.Collection.extend({
            model: Book,
        });
 
        var Library = new books();
 
        var b1 = new Book({
            title: "Ram",
            Author: "Amish Tripathi"
        });
         
        var b2 = new Book({
            title: "Lolita",
            Author: "Vladimir Nabokov"
        });
         
        Library.add(b1);
        Library.add(b2);
 
        document.write("Author Name of first Book is : ",
            Library.get('c1').get('Author'), '<br>');
        document.write("Author Name of second Book is : ",
            Library.get('c2').get('Author'));
    </script>
</body>
 
</html>


Output:

Backbone.js get Collection

Example 2: In this example, we will make our own unique identifier with the help of modeled models and use it as a unique identifier to get the model.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>BackboneJS modelId collection</title>
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
 
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>BackboneJS modelId collection</h3>
     
    <script type="text/javascript">
        var Book = Backbone.Model.extend();
 
        var books = Backbone.Collection.extend({
            model: Book,
            modelId: function (attr, o) {
                return attr.title + attr.vol;
            }
        });
 
        var Library = new books();
 
        var b1 = new Book({
            title: "Ram",
            Author: "Amish Tripathi",
            vol: 1
        });
         
        var b2 = new Book({
            title: "Beloved",
            Author: "Toni Morrison",
            vol: 1
        });
         
        Library.add(b1);
        Library.add(b2);
 
        document.write("Author Name of first Book is : ",
            Library.get('Ram1').get('Author'), '<br>');
        document.write("Author Name of second Book is : ",
            Library.get('Beloved1').get('Author'));
    </script>
</body>
 
</html>


Output:

Backbone.js set Collection

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

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32244 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6615 POSTS0 COMMENTS
Nicole Veronica
11787 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11832 POSTS0 COMMENTS
Shaida Kate Naidoo
6728 POSTS0 COMMENTS
Ted Musemwa
7009 POSTS0 COMMENTS
Thapelo Manthata
6684 POSTS0 COMMENTS
Umr Jansen
6697 POSTS0 COMMENTS