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