[Persistence] Handle overwrite/cancel

Handle Overwrite/Cancel more correctly when revision conflicts
are detected. WTD-1033.
This commit is contained in:
Victor Woeltjen
2015-03-20 14:39:47 -07:00
parent 513c06a81b
commit 1174f746f7
4 changed files with 59 additions and 19 deletions

View File

@ -22,13 +22,26 @@ define(
* @constructor
*/
function PersistenceCapability(persistenceService, SPACE, domainObject) {
// Cache modified timestamp
var modified = domainObject.getModel().modified;
// Update a domain object's model upon refresh
function updateModel(model) {
modified = model.modified;
return domainObject.useCapability("mutation", function () {
return model;
});
}
// For refresh; update a domain object model, only if there
// are no unsaved changes.
function maybeUpdateModel(model) {
// Only update the model if there are no pending changes
if (domainObject.getModel().modified === modified) {
updateModel(model);
}
}
return {
/**
* Persist any changes which have been made to this
@ -37,12 +50,18 @@ define(
* if persistence is successful, and rejected
* if not.
*/
persist: function () {
persist: function (hard) {
return persistenceService.updateObject(
SPACE,
domainObject.getId(),
domainObject.getModel()
);
domainObject.getModel(),
{ check: !hard }
).then(function (value) {
if (value) {
modified = domainObject.getModel().modified;
}
return value;
});
},
/**
* Update this domain object to match the latest from
@ -50,12 +69,12 @@ define(
* @returns {Promise} a promise which will be resolved
* when the update is complete
*/
refresh: function () {
refresh: function (hard) {
return persistenceService.readObject(
SPACE,
domainObject.getId(),
{ cache: false } // Disallow cached reads
).then(updateModel);
).then(hard ? updateModel : maybeUpdateModel);
},
/**
* Get the space in which this domain object is persisted;