Backbone.Undo.js is very simple to handle. It's made to *just work*. However this simplicity can also have downsides as it might not be suitable for a very complex application.
In comparison to other Backbone-based undo-managers like *Backbone.memento*, you don't have to modify your models or collections to use Backbone.Undo.js. That makes it easy to not only include Backbone.Undo.js, but also to remove it again.
To detect an action, Backbone.Undo.js listens to the events Backbone triggeres automatically. You don't have to do anything. You don't have to `store()` or `restore()` certain states. Nothing.
In a sophisticated webapp one click of the user might trigger several Backbone-Events which are stored as several Undo-Actions within the Undo-Stack. If the user then calls `undo()` it shouldn't just undo the latest action, it should undo all the actions which were triggered by the user's click. Backbone.Undo.js has a way to figure out which actions belong together and then undoes/redoes all of them.
Like with all the other JavaScript-Libraries you only need to include Backbone.Undo.js into your webpage or webapp to make it available.
As Backbone.Undo.js depends on Backbone you need Backbone, which again depends on underscore.js (or lowdash.js) and jQuery (or zepto). Make sure to include all these files before Backbone.Undo.js as it relies on these libraries:
<scriptsrc="jquery.js"></script>
<scriptsrc="underscore.js"></script>
<scriptsrc="backbone.js"></script>
<!-- Backbone.Undo.js is included*after* those other libs -->
<scriptsrc="Backbone.Undo.js"></script>
### Backbone Version
Backbone.Undo.js was developed for Backbone 1.0.0 or higher.
### Underscore Version
Backbone.Undo.js was developed for Underscore 1.4.4 or higher.
## Integrating the UndoManager into your webpage / webapp
# Setting up your UndoManager
In order to set up you UndoManager you have to do the following steps:
// 1. Instantiate your UndoManager
var myUndoManager = new Backbone.UndoManager({
maximumStackLength: 100 // maximumStackLength determines how many actions
// are stored to be undone. Default is Infinity aka no limit at all. This
// attribute is optional. You don't need to pass anything to the constructor.
});
// 2. Register the models and collections you want to observe
var model = new Backbone.Model,
collection = new Backbone.Collection;
myUndoManager.register(model, collection); // You can pass several objects as arguments
// You can setting up your objects here. Changes won't be tracked yet.
model.set("foo", "bar");
collection.add([{"something": "blue"}]);
// These changes can't be undone.
// 3. Start tracking the changes
myUndoManager.startTracking(); // Everything that happens from now on, can be undone
`startTracking()` is not part of the register function, to initialize
### Problems that might occur
Backbone.Undo.js is not made to be called within your code. It has an internal mechanism which figures out which Undo-Actions were generated in the same call cycle.
This mechanism is great for usability (see Advantages). However this mechanism makes it impossible to call `undo()` or `redo()` within a codeblock. Imagine this: