Skip to content
Related Articles

Related Articles

Backbone.js destroy Model

Improve Article
Save Article
  • Last Updated : 15 Jun, 2022
Improve Article
Save Article

The Backbone.js destroy model is used to delete the data of the given model in the server by delegating the sync() method.

Syntax:

Backbone.Model.destroy(options)    

Parameters: This method accepts one parameter that is described below:

  • options: It accepts attribute parameters like id etc.

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://code.jquery.com/jquery-2.1.3.min.js” type=”text/javascript”></script>  
<script src=”http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js” type=”text/javascript”></script>  <script src=”http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js” type=”text/javascript”></script>

Example: In this example, we are creating a book model and deleting the model from the server.

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">
        Backbone.sync = function (mymethod, mymodel) {
            document.write(mymethod + ": " 
            + JSON.stringify(mymodel) + "<br>");
            mymodel.set('id', 45);
        };
  
        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();
        book.destroy();  
    </script>
</head>
  
<body></body>
  
</html>


Output:

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

Example: 2 In this example, we are creating a book model and deleting the model from the server. and perform save after destroy.

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">
        Backbone.sync = function (mymethod, mymodel) {
            document.write(mymethod + ": " 
            + JSON.stringify(mymodel) + "<br>");
            mymodel.set('id', 45);
        };
        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.destroy();
        book.save();
    </script>
</head>
  
<body></body>
  
</html>


Output:

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

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!