Open In App

Backbone.js modelId Collection

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Backbone.js modelId Collection is used to uniquely identify the models in the collection. By default, Collection uses idAttribute value of Model to identify the models. We can override our modelId function whose return value will be used by the collection to uniquely identify the Models. 

Syntax:

collection.modeId( attrs, idAttribute );

Parameters: 

  • attrs: These are attributes passed to the models.
  • idAttribute: It is idAttribute of model. 

Example 1: In this example, we will illustrate the Backbone.js modelId Collection. Here we will see the default modelId return idAttribute.

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;">
        GeeksforGeeks
    </h1>
 
    <h3>BackboneJS modelId 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("ModeId is : ", Library.modelId);
    </script>
</body>
 
</html>


Output:

Backbone.js modelId Collection

Example 2: In this example, we will customize modelId and make a unique identifier with the specified attribute of 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;">
        GeeksforGeeks
    </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: "Lolita",
            Author: "Vladimir Nabokov",
            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 first Book is : ",
            Library.get("Lolita1").get("Author")
        );
    </script>
</body>
 
</html>


Output:

Backbone.js modelId Collection

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads