[API] Move type toward a newer API

This commit is contained in:
Victor Woeltjen 2016-05-27 13:31:30 -07:00
parent a6996df3df
commit c4fec1af6a
4 changed files with 77 additions and 2 deletions

View File

@ -31,10 +31,15 @@
<script type="text/javascript">
require(['main'], function (mct) {
require([
'./tutorials/todo/todo',
'./tutorials/todo/bundle',
'./example/imagery/bundle',
'./example/eventGenerator/bundle',
'./example/generator/bundle'
], mct.start.bind(mct));
], function (todoPlugin) {
todoPlugin(mct);
mct.start();
})
});
</script>
<link rel="stylesheet" href="platform/commonUI/general/res/css/startup-base.css">

View File

@ -1,11 +1,29 @@
define(['EventEmitter'], function (EventEmitter) {
define([
'EventEmitter',
'legacyRegistry',
'./api/api'
], function (EventEmitter, legacyRegistry, api) {
function MCT() {
EventEmitter.call(this);
this.legacyBundle = { extensions: {} };
}
MCT.prototype = Object.create(EventEmitter.prototype);
Object.keys(api).forEach(function (k) {
MCT.prototype[k] = api[k];
});
MCT.prototype.type = function (key, type) {
var legacyDef = type.toLegacyDefinition();
legacyDef.key = key;
this.legacyBundle.extensions.types =
this.legacyBundle.extensions.types || [];
this.legacyBundle.extensions.types.push(legacyDef);
};
MCT.prototype.start = function () {
legacyRegistry.register('adapter', this.legacyBundle);
this.emit('start');
};

43
src/api/Type.js Normal file
View File

@ -0,0 +1,43 @@
define(function () {
/**
* @typedef TypeDefinition
* @property {Metadata} metadata displayable metadata about this type
* @property {function (object)} [initialize] a function which initializes
* the model for new domain objects of this type
* @property {boolean} [creatable] true if users should be allowed to
* create this type (default: false)
*/
/**
*
* @param {TypeDefinition} definition
* @constructor
*/
function Type(definition) {
this.definition = definition;
}
/**
* Get a definition for this type that can be registered using the
* legacy bundle format.
* @private
*/
Type.prototype.toLegacyDefinition = function () {
var def = {};
def.name = this.definition.metadata.label;
def.glyph = this.definition.metadata.glyph;
def.description = this.definition.metadata.description;
if (this.definition.initialize) {
def.model = {};
this.definition.initialize(def.model);
}
if (this.definition.creatable) {
def.features = ['creation'];
}
return def;
};
return Type;
});

9
src/api/api.js Normal file
View File

@ -0,0 +1,9 @@
define([
'./Type'
], function (
Type
) {
return {
Type: Type
};
});