Open In App

Backbone.js destroy Model

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:

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.




<!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.




<!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


Article Tags :