From 222f852af49a212a6c9c39a0bc39dd649b72ee5c Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 26 Mar 2017 19:13:05 -0700 Subject: [PATCH] [Edit] Only persist on mutation if model has actually changed locally. Fixes #1482 --- .../src/capabilities/PersistenceCapability.js | 14 +++++++--- .../src/runs/TransactingMutationListener.js | 27 ++++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/platform/core/src/capabilities/PersistenceCapability.js b/platform/core/src/capabilities/PersistenceCapability.js index c0a99abc1a..e30d45f7b8 100644 --- a/platform/core/src/capabilities/PersistenceCapability.js +++ b/platform/core/src/capabilities/PersistenceCapability.js @@ -148,13 +148,19 @@ define( */ PersistenceCapability.prototype.refresh = function () { var domainObject = this.domainObject; + var $q = this.$q; // Update a domain object's model upon refresh function updateModel(model) { - var modified = model.modified; - return domainObject.useCapability("mutation", function () { - return model; - }, modified); + if (model === undefined) { + //Get failed, reject promise + return $q.reject('Got empty object model'); + } else { + var modified = model.modified; + return domainObject.useCapability("mutation", function () { + return model; + }, modified); + } } if (domainObject.getModel().persisted === undefined) { diff --git a/platform/core/src/runs/TransactingMutationListener.js b/platform/core/src/runs/TransactingMutationListener.js index d9f6d074f7..c242ed3015 100644 --- a/platform/core/src/runs/TransactingMutationListener.js +++ b/platform/core/src/runs/TransactingMutationListener.js @@ -34,23 +34,32 @@ define([], function () { transactionService, cacheService ) { + + function hasChanged(domainObject) { + var model = domainObject.getModel(); + return model.persisted === undefined || model.modified > model.persisted; + } + var mutationTopic = topic('mutation'); mutationTopic.listen(function (domainObject) { var persistence = domainObject.getCapability('persistence'); var wasActive = transactionService.isActive(); cacheService.put(domainObject.getId(), domainObject.getModel()); - if (!wasActive) { - transactionService.startTransaction(); - } + if (hasChanged(domainObject)) { - transactionService.addToTransaction( - persistence.persist.bind(persistence), - persistence.refresh.bind(persistence) - ); + if (!wasActive) { + transactionService.startTransaction(); + } - if (!wasActive) { - transactionService.commit(); + transactionService.addToTransaction( + persistence.persist.bind(persistence), + persistence.refresh.bind(persistence) + ); + + if (!wasActive) { + transactionService.commit(); + } } }); }