diff --git a/platform/core/bundle.json b/platform/core/bundle.json index 5a2727eb75..97ee2d262f 100644 --- a/platform/core/bundle.json +++ b/platform/core/bundle.json @@ -165,6 +165,10 @@ "implementation": "capabilities/PersistenceCapability.js", "depends": [ "persistenceService", "PERSISTENCE_SPACE" ] }, + { + "key": "metadata", + "implementation": "capabilities/MetadataCapability.js" + }, { "key": "mutation", "implementation": "capabilities/MutationCapability.js", diff --git a/platform/core/src/capabilities/MetadataCapability.js b/platform/core/src/capabilities/MetadataCapability.js new file mode 100644 index 0000000000..680e8b9fb1 --- /dev/null +++ b/platform/core/src/capabilities/MetadataCapability.js @@ -0,0 +1,62 @@ +/*global define*/ + +define( + function () { + "use strict"; + + /** + * A piece of information about a domain object. + * @typedef {Object} MetadataProperty + * @property {string} name the human-readable name of this property + * @property {string} value the human-readable value of this property, + * for this specific domain object + */ + + /** + * Implements the `metadata` capability of a domain object, providing + * properties of that object for display. + * + * Usage: `domainObject.useCapability("metadata")` + * + * ...which will return an array of objects containing `name` and + * `value` properties describing that domain object (suitable for + * display.) + * + * @constructor + */ + function MetadataCapability(domainObject) { + return { + /** + * Get metadata about this object. + * @returns {MetadataProperty[]} metadata about this object + */ + invoke: function () { + var type = domainObject.getCapability('type'), + model = domainObject.getModel(), + metadata = [{ + name: "ID", + value: domainObject.getId() + }]; + + function addProperty(typeProperty) { + var name = typeProperty.getDefinition().name, + value = typeProperty.getValue(model); + if (typeof value === 'string' || + typeof value === 'number') { + metadata.push({ + name: name, + value: value + }); + } + } + + (type ? type.getProperties() : []).forEach(addProperty); + + return metadata; + } + }; + } + + return MetadataCapability; + } +);