2014-12-05 12:48:21 -08:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
/**
|
|
|
|
* The EditRepresenter is responsible for implementing
|
|
|
|
* representation-level behavior relevant to Edit mode.
|
|
|
|
* Specifically, this listens for changes to view configuration
|
|
|
|
* or to domain object models, and triggers persistence when
|
|
|
|
* these are detected.
|
|
|
|
*
|
|
|
|
* This is exposed as an extension of category `representers`,
|
|
|
|
* which mct-representation will utilize to add additional
|
|
|
|
* behavior to each representation.
|
|
|
|
*
|
|
|
|
* This will be called once per mct-representation directive,
|
|
|
|
* and may be reused for different domain objects and/or
|
|
|
|
* representations resulting from changes there.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-12-06 09:38:28 -08:00
|
|
|
function EditRepresenter($q, $log, scope) {
|
|
|
|
var domainObject,
|
2014-12-05 12:48:21 -08:00
|
|
|
key;
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Mutate and persist a new version of a domain object's model.
|
2014-12-05 12:48:21 -08:00
|
|
|
function doPersist(model) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// First, mutate; then, persist.
|
2014-12-05 16:27:32 -08:00
|
|
|
return $q.when(domainObject.useCapability("mutation", function () {
|
|
|
|
return model;
|
|
|
|
})).then(function (result) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Only persist when mutation was successful
|
2014-12-05 12:48:21 -08:00
|
|
|
return result &&
|
|
|
|
domainObject.getCapability("persistence").persist();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Handle changes to model and/or view configuration
|
2014-12-06 09:38:28 -08:00
|
|
|
function commit(message) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Look up from scope; these will have been populated by
|
|
|
|
// mct-representation.
|
2014-12-05 12:48:21 -08:00
|
|
|
var model = scope.model,
|
2014-12-05 16:27:32 -08:00
|
|
|
configuration = scope.configuration;
|
2014-12-05 12:48:21 -08:00
|
|
|
|
2014-12-06 09:38:28 -08:00
|
|
|
// Log the commit message
|
|
|
|
$log.debug([
|
|
|
|
"Committing ",
|
|
|
|
domainObject && domainObject.getModel().name,
|
|
|
|
"(" + (domainObject && domainObject.getId()) + "):",
|
|
|
|
message
|
|
|
|
].join(" "));
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Update the configuration stored in the model, and persist.
|
2014-12-05 12:48:21 -08:00
|
|
|
if (domainObject && domainObject.hasCapability("persistence")) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Configurations for specific views are stored by
|
|
|
|
// key in the "configuration" field of the model.
|
2014-12-05 16:27:32 -08:00
|
|
|
if (key && configuration) {
|
|
|
|
model.configuration = model.configuration || {};
|
|
|
|
model.configuration[key] = configuration;
|
|
|
|
}
|
2014-12-05 12:48:21 -08:00
|
|
|
doPersist(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Respond to the destruction of the current representation.
|
2014-12-05 12:48:21 -08:00
|
|
|
function destroy() {
|
2014-12-06 09:38:28 -08:00
|
|
|
// No op
|
2014-12-05 12:48:21 -08:00
|
|
|
}
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Handle a specific representation of a specific domain object
|
2014-12-05 12:48:21 -08:00
|
|
|
function represent(representation, representedObject) {
|
2014-12-05 18:01:38 -08:00
|
|
|
// Track the key, to know which view configuration to save to.
|
2014-12-05 12:48:21 -08:00
|
|
|
key = representation.key;
|
2014-12-05 18:01:38 -08:00
|
|
|
// Track the represented object
|
2014-12-05 12:48:21 -08:00
|
|
|
domainObject = representedObject;
|
|
|
|
|
2014-12-05 18:01:38 -08:00
|
|
|
// Ensure existing watches are released
|
|
|
|
destroy();
|
2014-12-05 12:48:21 -08:00
|
|
|
}
|
|
|
|
|
2014-12-06 09:38:28 -08:00
|
|
|
// Place the "commit" method in the scope
|
|
|
|
scope.commit = commit;
|
|
|
|
|
2014-12-05 12:48:21 -08:00
|
|
|
return {
|
2014-12-05 18:01:38 -08:00
|
|
|
/**
|
|
|
|
* Set the current representation in use, and the domain
|
|
|
|
* object being represented.
|
|
|
|
*
|
|
|
|
* @param {RepresentationDefinition} representation the
|
|
|
|
* definition of the representation in use
|
|
|
|
* @param {DomainObject} domainObject the domain object
|
|
|
|
* being represented
|
|
|
|
*/
|
2014-12-05 12:48:21 -08:00
|
|
|
represent: represent,
|
2014-12-05 18:01:38 -08:00
|
|
|
/**
|
|
|
|
* Release any resources associated with this representer.
|
|
|
|
*/
|
2014-12-05 12:48:21 -08:00
|
|
|
destroy: destroy
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return EditRepresenter;
|
|
|
|
}
|
|
|
|
);
|