diff --git a/Backbone.Undo.js b/Backbone.Undo.js index a662b16..6180760 100644 --- a/Backbone.Undo.js +++ b/Backbone.Undo.js @@ -747,6 +747,15 @@ */ removeUndoType: function (type) { manipulateUndoType(2, type, undefined, this.undoTypes); + }, + + /** + * Removes all actions from the stack. + * @return {undefined} + */ + clear: function() { + this.stack.reset(); + this.stack.pointer = -1; } }); diff --git a/README.md b/README.md index 925346c..ee92b2e 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,10 @@ This changes an UndoType only on this specific undo manager and won't affect oth This removes an UndoType only from from this specific undo manager. See the UndoTypes-API for a more thorough documentation on this function. +#### clear `undoManager.clear()` + +This removes all actions from the stack of actions. + *** Methods you can call on the object `Backbone.UndoManager`: diff --git a/Tests/Backbone.Undo.Tests.js b/Tests/Backbone.Undo.Tests.js index a016bfb..8587fba 100644 --- a/Tests/Backbone.Undo.Tests.js +++ b/Tests/Backbone.Undo.Tests.js @@ -272,6 +272,40 @@ test("Merging UndoManagers", 2, function () { deepEqual(main.stack.at(1).toJSON().after, {"t": 2}, "The main undomanager can still write on its own stack") }) +test("Clearing all actions", function () { + var model = new Backbone.Model({ + "t": 1 + }); + + var UndoManager = new Backbone.UndoManager({ + track: true, + register: model + }); + + model.set("t", 2); + model.set("t", 3); + model.set("t", 4); + + UndoManager.clear(); + + UndoManager.undo(); + + deepEqual(model.toJSON(), {"t": 4}, "Clearing actions before undoing was successful"); + + model.set("t", 2); + model.set("t", 3); + model.set("t", 4); + + UndoManager.undo(); + UndoManager.undo(); + + UndoManager.clear(); + + UndoManager.redo(); + + deepEqual(model.toJSON(), {"t": 2}, "Clearing actions before redoing was successful"); +}) + /** * Async tests for magic condensation */