Wednesday, September 17, 2025
HomeLanguagesJavascriptBackbone.js previousAttributes Model

Backbone.js previousAttributes Model

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

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

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11833 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7050 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS