Merge pull request #1490 from nasa/open1482

[Edit] Only persist on mutation if model has actually changed locally
This commit is contained in:
Pete Richards 2017-04-27 11:28:11 -07:00 committed by GitHub
commit 058dfb0c87
2 changed files with 28 additions and 13 deletions

View File

@ -148,13 +148,19 @@ define(
*/ */
PersistenceCapability.prototype.refresh = function () { PersistenceCapability.prototype.refresh = function () {
var domainObject = this.domainObject; var domainObject = this.domainObject;
var $q = this.$q;
// Update a domain object's model upon refresh // Update a domain object's model upon refresh
function updateModel(model) { function updateModel(model) {
var modified = model.modified; if (model === undefined) {
return domainObject.useCapability("mutation", function () { //Get failed, reject promise
return model; return $q.reject('Got empty object model');
}, modified); } else {
var modified = model.modified;
return domainObject.useCapability("mutation", function () {
return model;
}, modified);
}
} }
if (domainObject.getModel().persisted === undefined) { if (domainObject.getModel().persisted === undefined) {

View File

@ -34,23 +34,32 @@ define([], function () {
transactionService, transactionService,
cacheService cacheService
) { ) {
function hasChanged(domainObject) {
var model = domainObject.getModel();
return model.persisted === undefined || model.modified > model.persisted;
}
var mutationTopic = topic('mutation'); var mutationTopic = topic('mutation');
mutationTopic.listen(function (domainObject) { mutationTopic.listen(function (domainObject) {
var persistence = domainObject.getCapability('persistence'); var persistence = domainObject.getCapability('persistence');
var wasActive = transactionService.isActive(); var wasActive = transactionService.isActive();
cacheService.put(domainObject.getId(), domainObject.getModel()); cacheService.put(domainObject.getId(), domainObject.getModel());
if (!wasActive) { if (hasChanged(domainObject)) {
transactionService.startTransaction();
}
transactionService.addToTransaction( if (!wasActive) {
persistence.persist.bind(persistence), transactionService.startTransaction();
persistence.refresh.bind(persistence) }
);
if (!wasActive) { transactionService.addToTransaction(
transactionService.commit(); persistence.persist.bind(persistence),
persistence.refresh.bind(persistence)
);
if (!wasActive) {
transactionService.commit();
}
} }
}); });
} }