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

Backbone.js clear Model

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

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