Open In App

Backbone.js clear Model

Last Updated : 13 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss Backbone.js clear model. The Backbone.js clear model is used to clear or remove all the existing attributes from the given model. The attributes will be set to undefined.

Syntax:

Backbone.Model.clear(options)

Parameters: It accepts a single parameter.

  • options: This parameter defines the parameters like id, name, and others (removed from the model).

Example 1: In this example, we will clear the book_name attribute that has already value, so we are clearing or removing the book_name value.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books(
          { book_name: "HTML", price: 100 }
        );
        document.write(
          "Actual Attribute:  ",
          book.get('book_name')
        );
        document.write("<br>");
  
        // Using the clear() method
        book.clear();
        document.write(
          "Attribute after Clear: ",
          book.get('book_name')
        );
    </script>
</body>
  
</html>


Output:

Actual Attribute: HTML
Attribute after Clear: undefined

Example 2: In this example, we will clear the price attribute that has already value, so we are clearing or removing the price value.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books(
          { book_name: "HTML", price: 100 }
        );
        document.write(
          "Actual Attribute:  ",
          book.get('price')
        );
        document.write("<br>");
  
        // Using the clear() method
        book.clear();
        document.write(
          "Attribute after Clear: ",
          book.get('price')
        );
    </script>
</body>
  
</html>


Output:

Actual Attribute: 100
Attribute after Clear: undefined

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads