Backbone.js unset Model

In this article, we will see the Backbone.js unset model. The Backbone.js unset model is used to unset or remove the value from the attributes in a given model.

Syntax:

Backbone.Model.unset(attribute);

Note: It takes one parameter.

  • attribute: specifies the attribute in a model to be unsettled.

Example 1: In this example, we will unset the bookid attribute in a book model

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
        type="text/javascript">
    </script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
        type="text/javascript">
    </script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
        type="text/javascript">
    </script>
  
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books({ 
            bookid: 23, 
            price: 678, 
            book_name: 'php' 
        });
  
        document.write("bookid:  ", book.get('bookid'));
          
        // Unset the bookid
        book.unset('bookid');
        document.write("<br>")
  
        document.write("bookid:  ", book.get('bookid')); 
    </script>
</head>
  
<body></body>
  
</html>


Output:

bookid: 23
bookid: undefined

Example 2: In this example, we will unset the price attribute in a book model

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
        type="text/javascript"></script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
        type="text/javascript"></script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
        type="text/javascript"></script>
  
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books({ 
            bookid: 23, 
            price: 678, 
            book_name: 'php' 
        });
  
        document.write("price:  ", book.get('price'));
  
        // Unset the bookid
        book.unset('price');
        document.write("<br>")
  
        document.write("price:  ", book.get('price')); 
    </script>
</head>
  
<body></body>
  
</html>


Output:

price: 678
price: undefined

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