Open In App

Backbone.js previousAttributes Model

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

The Backbone.js PreviousAttributes model is used to return the set of the given model’s previous attributes before the last change event. This model is beneficial to get the difference between the model’s version or restore it to the previous attributes after an error occurs.

Syntax:

Backbone.Model.previousAttributes(); 

It does not accept any parameter values.

Example 1: In this example, we are creating a model named orders and applying the PreviousAttributes model to the orders model after setting orderid.

HTML




<!DOCTYPE html>
<html>
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <script type="text/javascript">
      
        // Create the model
        var orders = new Backbone.Model({
              
            // Values for the model
            orderid: 180,
            ordername: 'clothes',
            address: 'guntur'
        });
          
        // Change the orderid 
        orders.set(180, 90);
          
        // Apply  previousAttributes
        document.write(JSON.stringify(
            orders.previousAttributes()));
    </script>
</body>
  
</html>


Output:

{"orderid":180,"ordername":"clothes","address":"guntur"}

Example 2: In this example, all the previous attribute for the story model before the changes are returned as the final output.

HTML




<!DOCTYPE html>
<html>
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <script>
        var story = new Backbone.Model({
            author: 'Ruskin Bond',
            book: 'Cherry Tree',
            Place: 'India'
        });
  
        story.set('book', 'School Time');
  
        document.write(JSON.stringify(
            story.previousAttributes()));
    </script>
</body>
</html>


Output:

{"author":"Ruskin Bond","book":"Cherry Tree","Place":"India"}

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads