[Persistence] Share transaction clearing functions

This commit is contained in:
Victor Woeltjen 2016-07-14 12:53:58 -07:00
parent 00fff52529
commit c7529dd56b

View File

@ -24,7 +24,7 @@
define( define(
[], [],
function () { function () {
var PERSIST_PENDING_SET = {}; var TRANSACTION_SET = {};
/** /**
* Wraps persistence capability to enable transactions. Transactions * Wraps persistence capability to enable transactions. Transactions
@ -49,15 +49,14 @@ define(
this.persistenceCapability = persistenceCapability; this.persistenceCapability = persistenceCapability;
this.domainObject = domainObject; this.domainObject = domainObject;
this.$q = $q; this.$q = $q;
this.removeFromTransaction = undefined;
} }
TransactionalPersistenceCapability.prototype.persistPending = function (state) { TransactionalPersistenceCapability.prototype.transactionClearer = function (state) {
var id = this.domainObject.getId(); var id = this.domainObject.getId();
if (arguments.length > 0) { if (arguments.length > 0) {
PERSIST_PENDING_SET[id] = state; TRANSACTION_SET[id] = state;
} }
return PERSIST_PENDING_SET[id]; return TRANSACTION_SET[id];
}; };
/** /**
@ -69,11 +68,11 @@ define(
var self = this; var self = this;
function onCommit() { function onCommit() {
if (!self.persistPending()) { if (!self.transactionClearer()) {
return Promise.resolve(true); return Promise.resolve(true);
} }
return self.persistenceCapability.persist().then(function (result) { return self.persistenceCapability.persist().then(function (result) {
self.persistPending(false); self.transactionClearer(undefined);
return result; return result;
}); });
} }
@ -82,11 +81,11 @@ define(
if (self.domainObject.getModel().persisted !== undefined) { if (self.domainObject.getModel().persisted !== undefined) {
//Fetch clean model from persistence //Fetch clean model from persistence
return self.persistenceCapability.refresh().then(function (result) { return self.persistenceCapability.refresh().then(function (result) {
self.persistPending(false); self.transactionClearer(undefined);
return result; return result;
}); });
} else { } else {
self.persistPending(false); self.transactionClearer(undefined);
self.removeFromTransaction = undefined; self.removeFromTransaction = undefined;
//Model is undefined in persistence, so return undefined. //Model is undefined in persistence, so return undefined.
return self.$q.when(undefined); return self.$q.when(undefined);
@ -94,10 +93,9 @@ define(
} }
if (this.transactionService.isActive()) { if (this.transactionService.isActive()) {
if (!self.persistPending()) { if (!self.transactionClearer()) {
this.removeFromTransaction = this.transactionService this.transactionClearer(this.transactionService
.addToTransaction(onCommit, onCancel); .addToTransaction(onCommit, onCancel));
this.persistPending(true);
} }
//Need to return a promise from this function //Need to return a promise from this function
return this.$q.when(true); return this.$q.when(true);
@ -107,10 +105,9 @@ define(
}; };
TransactionalPersistenceCapability.prototype.refresh = function () { TransactionalPersistenceCapability.prototype.refresh = function () {
if (this.persistPending()) { if (this.transactionClearer()) {
this.persistPending(false); this.transactionClearer()();
this.removeFromTransaction(); this.transactionClearer(undefined);
this.removeFromTransaction = undefined;
} }
return this.persistenceCapability.refresh(); return this.persistenceCapability.refresh();
}; };