Skip to content
Related Articles

Related Articles

Backbone.js save Model

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Jun, 2022

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!