Friday, January 10, 2025
Google search engine
HomeLanguagesJavascriptBackbone.js defaults Model

Backbone.js defaults Model

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;">
        neveropen
    </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;">
        neveropen
    </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

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

Recent Comments