From 2708562872aa2f8e78450fb408ebaabb449bc0b0 Mon Sep 17 00:00:00 2001 From: Pete Richards Date: Tue, 8 Nov 2016 10:34:20 -0800 Subject: [PATCH] [Transaction] Sync mutation within transaction --- .../commonUI/edit/src/actions/SaveAsAction.js | 4 ++- .../src/runs/TransactingMutationListener.js | 25 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/platform/commonUI/edit/src/actions/SaveAsAction.js b/platform/commonUI/edit/src/actions/SaveAsAction.js index 2befea07fd..ab10060e93 100644 --- a/platform/commonUI/edit/src/actions/SaveAsAction.js +++ b/platform/commonUI/edit/src/actions/SaveAsAction.js @@ -171,7 +171,9 @@ define([ function finishEditing(clonedObject) { return domainObject.getCapability("editor").finish() - .then(resolveWith(clonedObject)); + .then(function () { + return fetchObject(clonedObject.getId()); + }); } function onFailure() { diff --git a/platform/core/src/runs/TransactingMutationListener.js b/platform/core/src/runs/TransactingMutationListener.js index c534c6fae2..101798bd67 100644 --- a/platform/core/src/runs/TransactingMutationListener.js +++ b/platform/core/src/runs/TransactingMutationListener.js @@ -22,6 +22,8 @@ /*global define*/ define([], function () { + + var MUTATION_TRACKER = new WeakMap(); /** * Listens for mutation on domain objects and triggers persistence when * it occurs. @@ -37,10 +39,29 @@ define([], function () { if (!wasActive) { transactionService.startTransaction(); } + var wrap = function(f) { + return function () { + if (MUTATION_TRACKER.has(domainObject)) { + MUTATION_TRACKER.get(domainObject)(); + MUTATION_TRACKER.delete(domainObject); + } + return f(); + } + } + + if (!MUTATION_TRACKER.has(domainObject)) { + MUTATION_TRACKER.set(domainObject, domainObject + .getCapability('mutation') + .listen(function () {}) + ); + } + + // add model to cache and keep cache up to date with listener + // remove listener and remove from cache on commit & on cancel. transactionService.addToTransaction( - persistence.persist.bind(persistence), - persistence.refresh.bind(persistence) + wrap(persistence.persist.bind(persistence)), + wrap(persistence.refresh.bind(persistence)) ); if (!wasActive) {