[Layout] Add auto-save behavior

Add an EditRepresenter which detects changes to domain
object model or view configuration and, when these
occur, triggers mutation and persistence of the domain
object. (Note that in Edit mode, 'persistence' only
flags an object as dirty; actual persistence does not
occur until the user chooses Save.)

This supports editing of position/size in Layout mode,
and the persistence of those changes. WTD-535
This commit is contained in:
Victor Woeltjen 2014-12-05 12:48:21 -08:00
parent 67b9af54b3
commit a6de53c118
2 changed files with 68 additions and 0 deletions

View File

@ -89,6 +89,12 @@
"key": "topbar-edit",
"templateUrl": "templates/topbar-edit.html"
}
],
"representers": [
{
"implementation": "EditRepresenter.js",
"depends": [ "$q" ]
}
]
}
}

View File

@ -0,0 +1,62 @@
/*global define*/
define(
[],
function () {
"use strict";
function EditRepresenter($q, scope, element, attrs) {
var watches = [],
domainObject,
key;
function doPersist(model) {
return $q.when(function () {
return domainObject.useCapability("mutation", function () {
return model;
});
}).then(function (result) {
return result &&
domainObject.getCapability("persistence").persist();
});
}
function update() {
var model = scope.model,
configuration = scope.configuration,
key = scope.key;
if (domainObject && domainObject.hasCapability("persistence")) {
model.configuration = model.configuration || {};
model.configuration[key] = configuration;
doPersist(model);
}
}
function destroy() {
// Stop watching for changes
watches.forEach(function (deregister) { deregister(); });
watches = [];
}
function represent(representation, representedObject) {
key = representation.key;
domainObject = representedObject;
destroy(); // Ensure existing watches are released
watches = representedObject.hasCapability("editor") ? [
scope.$watch("model", update, true),
scope.$watch("configuration", update, true)
] : [];
}
return {
represent: represent,
destroy: destroy
};
}
return EditRepresenter;
}
);