Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js save Model

Backbone.js save Model

In this article, we will see the Backbone.js save() model. The Backbone.js save() model is used to save the data of the given model by delegating the sync() method. Whenever the backbone is called, every time it reads & saves the model.

Syntax:

Backbone.Model.save(attributes, [options]);

Parameters: It accepts two parameter values:

  • attributes: It specifies the attribute in the model.
  • options: This parameter accepts attribute parameters like id etc.

Example 1: In this example, we are creating a model named “book” and applying the save() model.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
  
<body>
    <script type="text/javascript">
        Backbone.sync = function (mymethod, mymodel) {
            document.write(mymethod + ": " 
            + JSON.stringify(mymodel) + "<br>");
  
        };
        var Books = Backbone.Model.extend();
        var book = new Books({ 
            bookid: 23, 
            price: 678, 
            book_name: "css" 
        });
  
        document.write(' Values: ' + JSON.stringify(book));
        document.write("<br>");
        book.save();  
    </script>
</body>
  
</html>


Output:

Values: {"bookid":23,"price":678,"book_name":"css"}
create: {"bookid":23,"price":678,"book_name":"css"}

Example 2: The following example demonstrates the save() model for empty “book” model data.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>    
</head>
  
<body>
    <script type="text/javascript"
      
        Backbone.sync = function(mymethod, mymodel) {  
           document.write(mymethod + ": " 
           + JSON.stringify(mymodel) + "<br>");  
        };  
        var Books = Backbone.Model.extend();  
        var book = new Books(); 
             
        document.write(' Values: ' + JSON.stringify(book)); 
        document.write("<br>");             
              
        book.save();  
    </script
</body>
</html>


Output:

Values: {}
create: {}

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

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

Most Popular

Recent Comments