Auto discover connector classes. Support a setup method

This commit is contained in:
Robin Ward 2016-12-12 13:07:33 -05:00
parent 9a7998a930
commit b6e03116ba
5 changed files with 38 additions and 25 deletions

View File

@ -8,6 +8,9 @@ export default Ember.Component.extend({
const args = this.get('args') || {};
Object.keys(args).forEach(key => this.set(key, args[key]));
const connectorClass = this.get('connector.connectorClass');
connectorClass.setupComponent.call(this, args, this);
},
send(name, ...args) {

View File

@ -42,7 +42,7 @@ export default Ember.Component.extend({
if (name) {
const args = this.get('args');
const connectors = connectorsFor(name).filter(con => {
return con.connectorClass.shouldRender(args);
return con.connectorClass.shouldRender(args, this);
});
this.set('connectors', connectors);

View File

@ -1,8 +1,10 @@
let _connectorCache;
let _extraConnectorClasses = {};
let _classPaths;
export function resetExtraClasses() {
_extraConnectorClasses = {};
_classPaths = undefined;
}
// Note: In plugins, define a class by path and it will be wired up automatically
@ -13,7 +15,8 @@ export function extraConnectorClass(name, obj) {
const DefaultConnectorClass = {
actions: {},
shouldRender: () => true
shouldRender: () => true,
setupComponent() { }
};
function findOutlets(collection, callback) {
@ -41,22 +44,33 @@ export function clearCache() {
_connectorCache = null;
}
function findClass(outletName, uniqueName) {
if (!_classPaths) {
_classPaths = {};
findOutlets(require._eak_seen, (outlet, res, un) => {
_classPaths[`${outlet}/${un}`] = require(res).default;
});
}
const id = `${outletName}/${uniqueName}`;
let foundClass = _extraConnectorClasses[id] || _classPaths[id];
return foundClass ?
jQuery.extend({}, DefaultConnectorClass, foundClass) :
DefaultConnectorClass;
}
function buildConnectorCache() {
_connectorCache = {};
findOutlets(Ember.TEMPLATES, function(outletName, resource, uniqueName) {
_connectorCache[outletName] = _connectorCache[outletName] || [];
const foundClass = _extraConnectorClasses[`${outletName}/${uniqueName}`];
const connectorClass = foundClass ?
jQuery.extend({}, DefaultConnectorClass, foundClass) :
DefaultConnectorClass;
_connectorCache[outletName].push({
templateName: resource.replace('javascripts/', ''),
template: Ember.TEMPLATES[resource],
classNames: `${outletName}-outlet ${uniqueName}`,
connectorClass
connectorClass: findClass(outletName, uniqueName)
});
});
}

View File

@ -11,26 +11,22 @@ export default function(name, opts) {
const controllerName = opts.admin ? `modals/${name}` : name;
const viewClass = container.lookupFactory('view:' + name);
const controller = container.lookup('controller:' + controllerName);
if (viewClass) {
route.render(name, { into: 'modal', outlet: 'modalBody' });
} else {
const templateName = opts.templateName || Ember.String.dasherize(name);
const templateName = opts.templateName || Ember.String.dasherize(name);
const renderArgs = { into: 'modal', outlet: 'modalBody'};
if (controller) { renderArgs.controller = controllerName; }
const renderArgs = { into: 'modal', outlet: 'modalBody'};
if (controller) { renderArgs.controller = controllerName; }
if (opts.addModalBodyView) {
renderArgs.view = 'modal-body';
}
if (opts.addModalBodyView) {
renderArgs.view = 'modal-body';
}
const modalName = `modal/${templateName}`;
const fullName = opts.admin ? `admin/templates/${modalName}` : modalName;
route.render(fullName, renderArgs);
if (opts.title) {
modalController.set('title', I18n.t(opts.title));
}
const modalName = `modal/${templateName}`;
const fullName = opts.admin ? `admin/templates/${modalName}` : modalName;
route.render(fullName, renderArgs);
if (opts.title) {
modalController.set('title', I18n.t(opts.title));
}
if (controller) {

View File

@ -17,7 +17,7 @@ function inject() {
}
function injectAll(app, name) {
inject(app, name, 'controller', 'component', 'route', 'view', 'model', 'adapter');
inject(app, name, 'controller', 'component', 'route', 'model', 'adapter');
}
export default {