Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js previous Model

Backbone.js previous Model

The Backbone.js previous model is used to get the previous value of the changed attributes, while the change event occurs in a given model. It will return the actual attribute of the model.

Syntax:

Backbone.Model.previous(attribute);

Parameter Value: It accepts one parameter, which is described below:

  • attribute: This parameter specifies the model’s property.

Example 1: In this example, we are creating a model named orders and changing orderid. After that, we are applying the previous model to return the previous value.

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">
    var orders = new Backbone.Model({
        orderid: 180,
        ordername: 'clothes',
        address: 'guntur'
    });
    orders.set('orderid', 21);
    document.write(JSON.stringify(orders.previous('orderid')));
    </script>
</body>
</html>


Output:

180

Example 2: In this example, the item value is changed & accordingly displays both the values i.e. the last value & the changed value.

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">
        var Fruit = new Backbone.Model({
            item: "Grape",
            taste: "sweet."
        });
        Fruit.set('item', 'GRAPES');
        document.write("Item's value after set: ", 
            JSON.stringify(Fruit.changedAttributes()));
        document.write("<br>");
        document.write("Item's Last value: ",
            Fruit.previous('item'));
    </script>
</body>
</html>


Output:

Item's value after set: {"item":"GRAPES"}
Item's Last value: Grape

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

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