Backbone.js previousAttributes Model
Last Updated :
13 Jun, 2022
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=
"https://code.jquery.com/jquery-2.1.3.min.js"
type="text/javascript">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
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=
"https://code.jquery.com/jquery-2.1.3.min.js"
type="text/javascript">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
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
Similar Reads
Computer Science Subjects