diff --git a/platform/commonUI/edit/bundle.js b/platform/commonUI/edit/bundle.js index 5a5101f129..fb357c0934 100644 --- a/platform/commonUI/edit/bundle.js +++ b/platform/commonUI/edit/bundle.js @@ -271,7 +271,8 @@ define([ "depends": [ "$q", "transactionService" - ] + ], + "priority": "fallback" }, { "type": "provider", diff --git a/platform/commonUI/edit/src/capabilities/TransactionalPersistenceCapability.js b/platform/commonUI/edit/src/capabilities/TransactionalPersistenceCapability.js index 0c6420d484..99ab8b6721 100644 --- a/platform/commonUI/edit/src/capabilities/TransactionalPersistenceCapability.js +++ b/platform/commonUI/edit/src/capabilities/TransactionalPersistenceCapability.js @@ -49,6 +49,7 @@ define( this.persistenceCapability = persistenceCapability; this.domainObject = domainObject; this.$q = $q; + this.persistPending = false; } /** @@ -57,11 +58,27 @@ define( * @returns {*} */ TransactionalPersistenceCapability.prototype.persist = function () { + var self = this; + + function onCommit() { + return self.persistenceCapability.persist().then(function(result) { + self.persistPending = false; + return result; + }); + } + + function onCancel() { + return self.persistenceCapability.refresh().then(function(result) { + self.persistPending = false; + return result; + }); + } + if (this.transactionService.isActive()) { - this.transactionService.addToTransaction( - this.persistenceCapability.persist.bind(this.persistenceCapability), - this.persistenceCapability.refresh.bind(this.persistenceCapability) - ); + if (!this.persistPending) { + this.transactionService.addToTransaction(onCommit, onCancel); + this.persistPending = true; + } //Need to return a promise from this function return this.$q.when(true); } else { diff --git a/platform/commonUI/edit/src/services/TransactionService.js b/platform/commonUI/edit/src/services/TransactionService.js index c6e0bcb5da..8d57e1e809 100644 --- a/platform/commonUI/edit/src/services/TransactionService.js +++ b/platform/commonUI/edit/src/services/TransactionService.js @@ -141,7 +141,7 @@ define( }; TransactionService.prototype.size = function () { - return this.onCommits.length + this.onCancels.length; + return this.onCommits.length; }; return TransactionService; diff --git a/platform/commonUI/edit/test/capabilities/EditorCapabilitySpec.js b/platform/commonUI/edit/test/capabilities/EditorCapabilitySpec.js index 29b08a4038..fe42ec92f7 100644 --- a/platform/commonUI/edit/test/capabilities/EditorCapabilitySpec.js +++ b/platform/commonUI/edit/test/capabilities/EditorCapabilitySpec.js @@ -55,6 +55,7 @@ define( "transactionService", [ "startTransaction", + "size", "commit", "cancel" ] @@ -161,11 +162,9 @@ define( }); it("returns true if the object has been modified since it" + " was last persisted", function () { - model.modified = 0; - model.persisted = 0; + mockTransactionService.size.andReturn(0); expect(capability.dirty()).toBe(false); - - model.modified = 1; + mockTransactionService.size.andReturn(1); expect(capability.dirty()).toBe(true); }); }); diff --git a/platform/commonUI/edit/test/capabilities/TransactionalPersistenceCapabilitySpec.js b/platform/commonUI/edit/test/capabilities/TransactionalPersistenceCapabilitySpec.js index baa870934d..8856aca4d5 100644 --- a/platform/commonUI/edit/test/capabilities/TransactionalPersistenceCapabilitySpec.js +++ b/platform/commonUI/edit/test/capabilities/TransactionalPersistenceCapabilitySpec.js @@ -56,7 +56,8 @@ define( "persistenceCapability", ["persist", "refresh"] ); - + mockPersistence.persist.andReturn(fastPromise()); + mockPersistence.refresh.andReturn(fastPromise()); capability = new TransactionalPersistenceCapability(mockQ, mockTransactionService, mockPersistence, mockDomainObject); }); @@ -67,26 +68,26 @@ define( expect(mockPersistence.persist).toHaveBeenCalled(); }); - it("if transaction is active, persist call is queued", function() { + it("if transaction is active, persist and cancel calls are" + + " queued", function() { mockTransactionService.isActive.andReturn(true); capability.persist(); expect(mockTransactionService.addToTransaction).toHaveBeenCalled(); - - //Test that it was the persist call that was queued mockTransactionService.addToTransaction.mostRecentCall.args[0](); expect(mockPersistence.persist).toHaveBeenCalled(); - }); - - it("if transaction is active, refresh call is queued as cancel" + - " function", function() { - mockTransactionService.isActive.andReturn(true); - capability.persist(); - - //Test that it was the persist call that was queued mockTransactionService.addToTransaction.mostRecentCall.args[1](); expect(mockPersistence.refresh).toHaveBeenCalled(); }); + it("persist call is only added to transaction once", function() { + mockTransactionService.isActive.andReturn(true); + capability.persist(); + expect(mockTransactionService.addToTransaction).toHaveBeenCalled(); + capability.persist(); + expect(mockTransactionService.addToTransaction.calls.length).toBe(1); + + }); + }); } ); \ No newline at end of file diff --git a/platform/commonUI/edit/test/services/TransactionServiceSpec.js b/platform/commonUI/edit/test/services/TransactionServiceSpec.js index 6c057aa9bd..c9f9746c5f 100644 --- a/platform/commonUI/edit/test/services/TransactionServiceSpec.js +++ b/platform/commonUI/edit/test/services/TransactionServiceSpec.js @@ -62,6 +62,17 @@ define( expect(transactionService.onCancels.length).toBe(1); }); + it("size function returns size of commit and cancel queues", function () { + var onCommit = jasmine.createSpy('onCommit'), + onCancel = jasmine.createSpy('onCancel'); + + transactionService.startTransaction(); + transactionService.addToTransaction(onCommit, onCancel); + transactionService.addToTransaction(onCommit, onCancel); + transactionService.addToTransaction(onCommit, onCancel); + expect(transactionService.size()).toBe(3); + }); + describe("commit", function () { var onCommits; diff --git a/platform/representation/test/MCTRepresentationSpec.js b/platform/representation/test/MCTRepresentationSpec.js index 36372a66c4..52d6c70d55 100644 --- a/platform/representation/test/MCTRepresentationSpec.js +++ b/platform/representation/test/MCTRepresentationSpec.js @@ -236,7 +236,7 @@ define( expect(mockLog.warn).toHaveBeenCalled(); }); - it("clears out obsolete peroperties from scope", function () { + it("clears out obsolete properties from scope", function () { mockScope.key = "def"; mockScope.domainObject = mockDomainObject; mockDomainObject.useCapability.andReturn("some value"); @@ -253,6 +253,21 @@ define( expect(mockScope.testCapability).toBeUndefined(); }); + it("registers a status change listener", function () { + mockScope.$watch.calls[2].args[1](mockDomainObject); + expect(mockStatusCapability.listen).toHaveBeenCalled(); + }); + + it("unlistens for status change on scope destruction", function () { + var mockUnlistener = jasmine.createSpy("unlisten"); + mockStatusCapability.listen.andReturn(mockUnlistener); + mockScope.$watch.calls[2].args[1](mockDomainObject); + expect(mockStatusCapability.listen).toHaveBeenCalled(); + + mockScope.$on.calls[1].args[1](); + expect(mockUnlistener).toHaveBeenCalled(); + }); + describe("when a domain object has been observed", function () { var mockContext, mockContext2, @@ -314,6 +329,7 @@ define( mockScope.$watch.calls[0].args[1](); expect(mockChangeTemplate.calls.length).toEqual(callCount); }); + });