[Layout] Add notion of representers

Add representers as a category of extension; these are extra
steps to perform when representing a domain object. This
will be used to support automatic mutation/persistence of
domain objects from a watch, while avoiding bloat within
the mct-representation directive itself. This, in turn,
simplifies the persistence strategy to be employed by
Layout views when editing concludes. WTD-535.
This commit is contained in:
Victor Woeltjen 2014-12-05 09:43:22 -08:00
parent b50f40f399
commit 67b9af54b3
3 changed files with 62 additions and 14 deletions

View File

@ -9,7 +9,7 @@
{
"key": "mctRepresentation",
"implementation": "MCTRepresentation.js",
"depends": [ "representations[]", "views[]", "gestureService", "$q", "$log" ]
"depends": [ "representations[]", "views[]", "representers[]", "$q", "$log" ]
}
],
"gestures": [
@ -36,6 +36,12 @@
"implementation": "gestures/GestureProvider.js",
"depends": ["gestures[]"]
}
],
"representers": [
{
"implementation": "gestures/GestureRepresenter.js",
"depends": [ "gestureService" ]
}
]
}
}

View File

@ -31,7 +31,7 @@ define(
* representation extensions
* @param {ViewDefinition[]} views an array of view extensions
*/
function MCTRepresentation(representations, views, gestureService, $q, $log) {
function MCTRepresentation(representations, views, representers, $q, $log) {
var pathMap = {},
representationMap = {},
gestureMap = {};
@ -52,8 +52,10 @@ define(
});
function link($scope, element) {
var gestureHandle;
function link($scope, element, attrs) {
var activeRepresenters = representers.map(function (Representer) {
return new Representer($scope, element, attrs);
});
// General-purpose refresh mechanism; should set up the scope
// as appropriate for current representation key and
@ -73,9 +75,9 @@ define(
$scope.inclusion = pathMap[$scope.key];
// Any existing gestures are no longer valid; release them.
if (gestureHandle) {
gestureHandle.destroy();
}
activeRepresenters.forEach(function (activeRepresenter) {
activeRepresenter.destroy();
});
// Log if a key was given, but no matching representation
// was found.
@ -89,6 +91,11 @@ define(
// Always provide the model, as "model"
$scope.model = domainObject.getModel();
// Also provide the view configuration,
// for the specific view
$scope.configuration =
($scope.model.configuration || {})[$scope.key] || {};
// Also provide any of the capabilities requested
uses.forEach(function (used) {
$log.debug([
@ -104,13 +111,11 @@ define(
});
});
// Finally, wire up any gestures that should be
// associated with this representation.
gestureHandle = gestureService.attachGestures(
element,
domainObject,
gestureKeys
);
// Finally, wire up any additional behavior (such as
// gestures) associated with this representation.
activeRepresenters.forEach(function (representer) {
representer.represent(representation, domainObject);
});
}
}

View File

@ -0,0 +1,37 @@
/*global define*/
define(
[],
function () {
"use strict";
function GestureRepresenter(gestureService, scope, element) {
var gestureHandle;
function destroy() {
if (gestureHandle) {
gestureHandle.destroy();
}
}
function represent(representation, domainObject) {
// Clear out any existing gestures
destroy();
// Attach gestures - by way of the service.
gestureHandle = gestureService.attachGestures(
element,
domainObject,
(representation || {}).gestures || []
);
}
return {
represent: represent,
destroy: destroy
};
}
return GestureRepresenter;
}
);