From 258f516ed932787fc3a5e1c970831a885e00872f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 19 Mar 2014 10:36:45 +0100 Subject: [PATCH 1/1] smartxml: Rollback support for transactions --- src/smartxml/smartxml.js | 10 ++++++++++ src/smartxml/smartxml.test.js | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 426dce0..31faef0 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -560,6 +560,7 @@ $.extend(Document.prototype, Backbone.Events, { if(this._currentTransaction) { throw new Error('Nested transactions not supported!'); } + this._rollbackBackup = this.root.clone(); this._currentTransaction = new Transaction([], metadata); }, @@ -573,6 +574,15 @@ $.extend(Document.prototype, Backbone.Events, { this._currentTransaction = null; }, + rollbackTransaction: function() { + if(!this._currentTransaction) { + throw new Error('Transaction rollback requested, but there is no transaction in progress!'); + } + this.replaceRoot(this._rollbackBackup); + this._rollbackBackup = null; + this._currentTransaction = null; + }, + transaction: function(callback, context, metadata) { var toret; this.startTransaction(metadata); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 8f7deb6..c792cf9 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -1649,6 +1649,17 @@ describe('smartxml', function() { var transaction = doc.undoStack[0]; expect(transaction.metadata).to.equal(metadata); }); + + it('can be rolled back', function() { + var doc = getDocumentFromXML(''); + + doc.startTransaction(); + doc.root.append({tagName: 'div'}); + doc.rollbackTransaction(); + + expect(doc.undoStack.length).to.equal(0, 'nothing to undo'); + expect(doc.root.contents().length).to.equal(0); + }); }); describe('Regression tests', function() { -- 2.20.1