HomeLanguagesJavascriptBackbone.js changed Model

Backbone.js changed Model

In this article, we will discuss Backbone.js changed model. The Backbone.js changed model is used to change the attributes that are changed after setting the attributes by using the set() method.

Syntax:

Backbone.Model.changed

Note: It takes no parameters.

Example 1: In this example, we will change the book_name:

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">
        Books = Backbone.Model.extend({
            defaults: {
                book_name: 'Intro to html',
                author: 'X'
            },
            initialize: function () {
                this.bind("update:book_name", function (model) {
                    var bname = model.get("book_name");
                    var bauthor = model.get("author");
                });
            }
        });
          
        var final = new Books();
        document.write("Actual Value: ",
            final.get("book_name"));
        document.write("<br>");
        final.set({ book_name: 'PHP Introduction' });
        document.write("Changed Value: ",
            final.get("book_name"));  
    </script>
</head>
  
</html>


Output:

Actual Value: Intro to html
Changed Value: PHP Introduction

Example 2: In this example, we will change the author

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">
        Books = Backbone.Model.extend({
            defaults: {
                book_name: 'Intro to html',
                author: 'X'
            },
            initialize: function () {
                this.bind("update:book_name", function (model) {
                    var bname = model.get("book_name");
                    var bauthor = model.get("author");
                });
            }
        });
        var final = new Books();
        document.write("Actual Value: ",
            final.get("author"));
        document.write("<br>");
        final.set({ author: 'Y' });
        document.write("Changed Value: ",
            final.get("author"));  
    </script>
</head>
  
<body></body>
  
</html>


Output:

Actual Value: X
Changed Value: Y

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

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

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS