[API] Move view off of type

This commit is contained in:
Victor Woeltjen 2016-06-17 10:53:56 -07:00
parent d891affe48
commit 2240a87ddc
3 changed files with 55 additions and 28 deletions

View File

@ -1,8 +1,14 @@
define([
'EventEmitter',
'legacyRegistry',
'uuid',
'./api/api'
], function (EventEmitter, legacyRegistry, api) {
], function (
EventEmitter,
legacyRegistry,
uuid,
api
) {
function MCT() {
EventEmitter.call(this);
this.legacyBundle = { extensions: {} };
@ -16,7 +22,41 @@ define([
MCT.prototype.MCT = MCT;
MCT.prototype.view = function (region, factory) {
var viewKey = region + uuid();
var adaptedViewKey = "adapted-view-" + viewKey;
this.legacyBundle.extensions.views =
this.legacyBundle.extensions.views || [];
this.legacyBundle.extensions.views.push({
name: "A view",
key: adaptedViewKey,
template: '<mct-view key="\'' +
viewKey +
'\'" ' +
'mct-object="domainObject">' +
'</mct-view>'
});
this.legacyBundle.extensions.policies =
this.legacyBundle.extensions.policies || [];
this.legacyBundle.extensions.policies.push({
category: "view",
implementation: function Policy() {
this.allow = function (view, domainObject) {
if (view.key === adaptedViewKey) {
return !!factory(domainObject);
}
return true;
};
}
});
this.legacyBundle.extensions.newViews =
this.legacyBundle.extensions.newViews || [];
this.legacyBundle.extensions.newViews.push({
factory: factory,
key: viewKey
});
};
MCT.prototype.type = function (key, type) {
@ -26,29 +66,7 @@ define([
this.legacyBundle.extensions.types || [];
this.legacyBundle.extensions.types.push(legacyDef);
var viewFactory = type.view(this.regions.main);
if (viewFactory) {
var viewKey = key + "." + this.regions.main;
this.legacyBundle.extensions.views =
this.legacyBundle.extensions.views || [];
this.legacyBundle.extensions.views.push({
name: "A view",
key: "adapted-view",
template: '<mct-view key="\'' +
viewKey +
'\'" ' +
'mct-object="domainObject">' +
'</mct-view>'
});
this.legacyBundle.extensions.newViews =
this.legacyBundle.extensions.newViews || [];
this.legacyBundle.extensions.newViews.push({
factory: viewFactory,
key: viewKey
});
}
type.key = key;
};
MCT.prototype.start = function () {

View File

@ -17,6 +17,16 @@ define(function () {
this.definition = definition;
}
/**
* Check if a domain object is an instance of this type.
* @param domainObject
* @returns {boolean} true if the domain object is of this type
*/
Type.prototype.check = function (domainObject) {
// Depends on assignment from MCT.
return domainObject.getModel().type === this.key;
};
/**
* Get a definition for this type that can be registered using the
* legacy bundle format.

View File

@ -97,11 +97,10 @@ define([
$message.toggle(tasks.length < 1);
};
todoType.view(mct.regions.main, function (domainObject) {
return new TodoView(domainObject);
});
mct.type('example.todo', todoType);
mct.view(mct.regions.main, function (domainObject) {
return todoType.check(domainObject) && new TodoView(domainObject);
});
return mct;
};