Open In App

Backbone.js defaults Model

Last Updated : 31 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Backbone.js defaults Model is a hash of function which is used to specify the default attributes for the Model. It is used when we create an instance of the model, and we didn’t specify any attribute then default attributes are used. 

Syntax: 

Backbone.model.defaults; 

Parameters: It takes default parameters which all models should have. 

Using the CDN Link: A content delivery network is a network that serves files to users. Here are the CDNs for Backbone.js

<script src= “https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.2/backbone-min.js” type=”text/javascript”> </script>

Example 1: In this example, we will illustrate defaults and see all instances have common if attributes are not specified.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>BackboneJS Model defaults</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 Model defaults</h3>
 
    <script type="text/javascript">
        var Geek = Backbone.Model.extend({
            defaults: {
                "id": -1,
                "Name": "Anonymous",
            }
        });;
 
        var geek = new Geek();
        var geek2 = new Geek();
 
        // First instance of Geek
        document.write(JSON.stringify(geek), '<br>')
 
        // Second instance of Geek
        document.write(JSON.stringify(geek2))
    </script>
</body>
 
</html>


Output:

default model

Example 2: In this example, we will see changes in default value attributes with user-specified values.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>BackboneJS Model defaults</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 Model defaults</h3>
 
    <script type="text/javascript">
        var Geek = Backbone.Model.extend({
            defaults: {
                "id": -1,
                "Name": "Anonymous",
            }
        });;
 
        var geek = new Geek();
        var geek2 = new Geek();
 
        // First instance of Geek
        document.write(JSON.stringify(geek), '<br>')
 
        // Second instance of Geek
        document.write(JSON.stringify(geek2))
    </script>
</body>
 
</html>


Output:

default model

Reference: https://backbonejs.org/#Model-defaults



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads