Clear cache when no transactions active

This commit is contained in:
Pete Richards 2016-11-08 13:50:13 -08:00
parent 42c48cb93b
commit f991dcfb76
3 changed files with 21 additions and 4 deletions

View File

@ -347,7 +347,8 @@ define([
"implementation": TransactionService, "implementation": TransactionService,
"depends": [ "depends": [
"$q", "$q",
"$log" "$log",
"cacheService"
] ]
}, },
{ {

View File

@ -34,9 +34,10 @@ define(
* @param $q * @param $q
* @constructor * @constructor
*/ */
function TransactionService($q, $log) { function TransactionService($q, $log, cacheService) {
this.$q = $q; this.$q = $q;
this.$log = $log; this.$log = $log;
this.cacheService = cacheService;
this.transactions = []; this.transactions = [];
} }
@ -87,14 +88,25 @@ define(
/** /**
* All persist calls deferred since the beginning of the transaction * All persist calls deferred since the beginning of the transaction
* will be committed. * will be committed. If this is the last transaction, clears the
* cache.
* *
* @returns {Promise} resolved when all persist operations have * @returns {Promise} resolved when all persist operations have
* completed. Will reject if any commit operations fail * completed. Will reject if any commit operations fail
*/ */
TransactionService.prototype.commit = function () { TransactionService.prototype.commit = function () {
var transaction = this.transactions.pop(); var transaction = this.transactions.pop();
return transaction ? transaction.commit() : Promise.reject(); if (!transaction) {
return Promise.reject();
}
if (!this.isActive()) {
return transaction.commit()
.then(function (r) {
this.cacheService.flush();
return r;
}.bind(this))
}
return transaction.commit();
}; };
/** /**

View File

@ -77,5 +77,9 @@ define([], function () {
return this.cache; return this.cache;
}; };
ModelCacheService.prototype.flush = function () {
this.cache = {};
};
return ModelCacheService; return ModelCacheService;
}); });