Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js destroy Model

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:

  • 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

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

Most Popular

Recent Comments