diff --git a/platform/commonUI/edit/bundle.json b/platform/commonUI/edit/bundle.json index 8e4ec2e18a..e5e9f0f90e 100644 --- a/platform/commonUI/edit/bundle.json +++ b/platform/commonUI/edit/bundle.json @@ -89,6 +89,12 @@ "key": "topbar-edit", "templateUrl": "templates/topbar-edit.html" } + ], + "representers": [ + { + "implementation": "EditRepresenter.js", + "depends": [ "$q" ] + } ] } } \ No newline at end of file diff --git a/platform/commonUI/edit/src/EditRepresenter.js b/platform/commonUI/edit/src/EditRepresenter.js new file mode 100644 index 0000000000..9db1327b1d --- /dev/null +++ b/platform/commonUI/edit/src/EditRepresenter.js @@ -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; + } +); \ No newline at end of file