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, zambiatek 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!


