[Persistence] Track persist-pending state globally

This commit is contained in:
Victor Woeltjen 2016-07-14 12:50:36 -07:00
parent 4c7ad6d93a
commit 00fff52529

View File

@ -24,6 +24,7 @@
define( define(
[], [],
function () { function () {
var PERSIST_PENDING_SET = {};
/** /**
* Wraps persistence capability to enable transactions. Transactions * Wraps persistence capability to enable transactions. Transactions
@ -48,10 +49,17 @@ define(
this.persistenceCapability = persistenceCapability; this.persistenceCapability = persistenceCapability;
this.domainObject = domainObject; this.domainObject = domainObject;
this.$q = $q; this.$q = $q;
this.persistPending = false;
this.removeFromTransaction = undefined; this.removeFromTransaction = undefined;
} }
TransactionalPersistenceCapability.prototype.persistPending = function (state) {
var id = this.domainObject.getId();
if (arguments.length > 0) {
PERSIST_PENDING_SET[id] = state;
}
return PERSIST_PENDING_SET[id];
};
/** /**
* The wrapped persist function. If a transaction is active, persist * The wrapped persist function. If a transaction is active, persist
* will be queued until the transaction is committed or cancelled. * will be queued until the transaction is committed or cancelled.
@ -61,8 +69,11 @@ define(
var self = this; var self = this;
function onCommit() { function onCommit() {
if (!self.persistPending()) {
return Promise.resolve(true);
}
return self.persistenceCapability.persist().then(function (result) { return self.persistenceCapability.persist().then(function (result) {
self.persistPending = false; self.persistPending(false);
return result; return result;
}); });
} }
@ -71,11 +82,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.persistPending(false);
return result; return result;
}); });
} else { } else {
self.persistPending = false; self.persistPending(false);
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);
@ -83,10 +94,10 @@ define(
} }
if (this.transactionService.isActive()) { if (this.transactionService.isActive()) {
if (!this.persistPending) { if (!self.persistPending()) {
this.removeFromTransaction = this.transactionService this.removeFromTransaction = this.transactionService
.addToTransaction(onCommit, onCancel); .addToTransaction(onCommit, onCancel);
this.persistPending = true; 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);
@ -96,8 +107,8 @@ define(
}; };
TransactionalPersistenceCapability.prototype.refresh = function () { TransactionalPersistenceCapability.prototype.refresh = function () {
if (this.persistPending) { if (this.persistPending()) {
this.persistPending = false; this.persistPending(false);
this.removeFromTransaction(); this.removeFromTransaction();
this.removeFromTransaction = undefined; this.removeFromTransaction = undefined;
} }