[Code Style] Use prototypes in platform

WTD-1482
This commit is contained in:
Victor Woeltjen
2015-08-11 12:54:50 -07:00
parent f377c7cb71
commit b7765ff388
35 changed files with 1331 additions and 1379 deletions

View File

@ -1,134 +0,0 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,Promise*/
/**
* Module defining DomainObject. Created by vwoeltje on 11/7/14.
*/
define(
[],
function () {
"use strict";
/**
* Construct a new domain object with the specified
* identifier, model, and capabilities.
*
* @param {string} id the object's unique identifier
* @param {object} model the "JSONifiable" state of the object
* @param {Object.<string, Capability>|function} capabilities all
* capabilities to be exposed by this object
* @memberof platform/core
* @constructor
*/
function DomainObject(id, model, capabilities) {
return {
/**
* Get the unique identifier for this domain object.
* @return {string} the domain object's unique identifier
* @memberof DomainObject#
* @memberof platform/core.DomainObject#
*/
getId: function () {
return id;
},
/**
* Get the domain object's model. This is useful to
* directly look up known properties of an object, but
* direct modification of a returned model is generally
* discouraged and may result in errors. Instead, an
* object's "mutation" capability should be used.
*
* @return {object} the domain object's persistent state
* @memberof DomainObject#
* @memberof platform/core.DomainObject#
*/
getModel: function () {
return model;
},
/**
* Get a capability associated with this object.
* Capabilities are looked up by string identifiers;
* prior knowledge of a capability's interface is
* necessary.
*
* @return {Capability} the named capability, or undefined
* if not present.
* @memberof DomainObject#
* @memberof platform/core.DomainObject#
*/
getCapability: function (name) {
var capability = capabilities[name];
return typeof capability === 'function' ?
capability(this) : capability;
},
/**g
* Check if this domain object supports a capability
* with the provided name.
*
* @param {string} name the name of the capability to
* check for
* @returns {boolean} true if provided
* @memberof platform/core.DomainObject#
*/
hasCapability: function hasCapability(name) {
return this.getCapability(name) !== undefined;
},
/**
* Use a capability of an object; this is a shorthand
* for:
*
* ```
* hasCapability(name) ?
* getCapability(name).invoke(args...) :
* undefined
* ```
*
* That is, it handles both the check-for-existence and
* invocation of the capability, and checks for existence
* before invoking the capability.
*
* @param {string} name the name of the capability to invoke
* @param {...*} [arguments] to pass to the invocation
* @returns {*}
* @memberof DomainObject#
* @memberof platform/core.DomainObject#
*/
useCapability: function (name) {
// Get tail of args to pass to invoke
var args = Array.prototype.slice.apply(arguments, [1]),
capability = this.getCapability(name);
return (capability && capability.invoke) ?
capability.invoke.apply(capability, args) :
capability;
}
};
}
return DomainObject;
}
);

View File

@ -0,0 +1,143 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,Promise*/
/**
* Module defining DomainObject. Created by vwoeltje on 11/7/14.
*/
define(
[],
function () {
"use strict";
/**
* A domain object is an entity of interest to the user.
*
* @interface DomainObject
*/
/**
* Get the unique identifier for this domain object.
*
* @method DomainObject#getId
* @return {string} the domain object's unique identifier
*/
/**
* Get the domain object's model. This is useful to
* directly look up known properties of an object, but
* direct modification of a returned model is generally
* discouraged and may result in errors. Instead, an
* object's `mutation` capability should be used.
*
* @method DomainObject#getModel
* @return {object} the domain object's persistent state
*/
/**
* Get a capability associated with this object.
* Capabilities are looked up by string identifiers;
* prior knowledge of a capability's interface is
* necessary.
*
* @method DomainObject#getCapability
* @param {string} key the identifier for the capability
* @return {Capability} the named capability, or undefined
* if not present.
*/
/**
* Check if this domain object supports a capability
* with the provided name.
*
* @method DomainObject#hasCapability
* @param {string} key the identifier for the capability
* @return {boolean} true if this domain object has this capability
*/
/**
* Use a capability of an object; the behavior of this method
* depends on the interface of the capability, and whether
* or not it is present.
*
* * If the capability is not present for this object,
* no operation occurs.
* * If the capability is present and has an `invoke` method,
* that method is called with any additional arguments
* provided, and its return value is returned.
* * If the capability is present but has no `invoke` method,
* this capability itself is returned.
*
* @method DomainObject#useCapability
* @param {string} name the name of the capability to invoke
* @param {...*} [arguments] to pass to the invocation
* @returns {*|Capability} the result of invocation (see description)
*/
/**
* Construct a new domain object with the specified
* identifier, model, and capabilities.
*
* @param {string} id the object's unique identifier
* @param {object} model the "JSONifiable" state of the object
* @param {Object.<string, Capability>|function} capabilities all
* capabilities to be exposed by this object
* @memberof platform/core
* @constructor
*/
function DomainObjectImpl(id, model, capabilities) {
this.id = id;
this.model = model;
this.capabilities = capabilities;
}
DomainObjectImpl.prototype.getId = function () {
return this.id;
};
DomainObjectImpl.prototype.getModel = function () {
return this.model;
};
DomainObjectImpl.prototype.getCapability = function (name) {
var capability = this.capabilities[name];
return typeof capability === 'function' ?
capability(this) : capability;
};
DomainObjectImpl.prototype.hasCapability = function (name) {
return this.getCapability(name) !== undefined;
};
DomainObjectImpl.prototype.useCapability = function (name) {
// Get tail of args to pass to invoke
var args = Array.prototype.slice.apply(arguments, [1]),
capability = this.getCapability(name);
return (capability && capability.invoke) ?
capability.invoke.apply(capability, args) :
capability;
};
return DomainObjectImpl;
}
);

View File

@ -27,10 +27,31 @@
* @namespace platform/core
*/
define(
["./DomainObject"],
function (DomainObject) {
["./DomainObjectImpl"],
function (DomainObjectImpl) {
"use strict";
/**
* Provides instances of domain objects, as retrieved by their
* identifiers.
*
* @interface ObjectService
*/
/**
* Get a set of objects associated with a list of identifiers.
* The provided result may contain a subset or a superset of
* the total number of objects.
*
* @method ObjectService#getObjects
* @param {string[]} ids the identifiers for domain objects
* of interest.
* @return {Promise<object<string, DomainObject>>} a promise
* for an object containing key-value pairs, where keys
* are string identifiers for domain objects, and
* values are the corresponding domain objects themselves.
*/
/**
* Construct a new provider for domain objects.
*
@ -44,6 +65,16 @@ define(
* @constructor
*/
function DomainObjectProvider(modelService, capabilityService, $q) {
this.modelService = modelService;
this.capabilityService = capabilityService;
this.$q = $q;
}
DomainObjectProvider.prototype.getObjects = function getObjects(ids) {
var modelService = this.modelService,
capabilityService = this.capabilityService,
$q = this.$q;
// Given a models object (containing key-value id-model pairs)
// create a function that will look up from the capability
// service based on id; for handy mapping below.
@ -51,8 +82,8 @@ define(
return function (id) {
var model = models[id];
return model ?
capabilityService.getCapabilities(model) :
undefined;
capabilityService.getCapabilities(model) :
undefined;
};
}
@ -65,7 +96,7 @@ define(
ids.forEach(function (id, index) {
if (models[id]) {
// Create the domain object
result[id] = new DomainObject(
result[id] = new DomainObjectImpl(
id,
models[id],
capabilities[index]
@ -75,36 +106,14 @@ define(
return result;
}
// Get object instances; this is the useful API exposed by the
// domain object provider.
function getObjects(ids) {
return modelService.getModels(ids).then(function (models) {
return $q.all(
ids.map(capabilityResolver(models))
).then(function (capabilities) {
return modelService.getModels(ids).then(function (models) {
return $q.all(
ids.map(capabilityResolver(models))
).then(function (capabilities) {
return assembleResult(ids, models, capabilities);
});
});
}
return {
/**
* Get a set of objects associated with a list of identifiers.
* The provided result may contain a subset or a superset of
* the total number of objects.
*
* @param {Array<string>} ids the identifiers for domain objects
* of interest.
* @return {Promise<object<string, DomainObject>>} a promise
* for an object containing key-value pairs, where keys
* are string identifiers for domain objects, and
* values are the corresponding domain objects themselves.
* @memberof module:core/object/object-provider.ObjectProvider#
* @memberof platform/core.DomainObjectProvider#
*/
getObjects: getObjects
};
}
});
};
return DomainObjectProvider;
}