From f3b02ea96c0a607b77bb7fbc7e0389748fb07c12 Mon Sep 17 00:00:00 2001 From: Rob Eisenberg Date: Sun, 21 Dec 2014 22:10:29 -0500 Subject: [PATCH] feat(framework): enable plugin loading and config A new api has been added to the aurelia class called withPlugin which allows you to specify a module id and a config. When you call aurelia.start all plugins will be dynamically loaded and their install methods will be called with the config object. install is options. If present, it may return a promise. --- src/aurelia.js | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/aurelia.js b/src/aurelia.js index d9c9b124..f4932419 100644 --- a/src/aurelia.js +++ b/src/aurelia.js @@ -6,12 +6,33 @@ import {EventAggregator, includeEventsIn} from 'aurelia-event-aggregator'; var logger = LogManager.getLogger('aurelia'); +function loadPlugins(loader, plugins){ + var toLoad = [], i, ii, current; + + for(i = 0, ii = plugins.length; i < ii; ++i){ + current = plugins[i]; + logger.debug(`Loading plugin ${current.moduleId}.`); + toLoad.push(loader.loadModule(current.moduleId).then(exportedValue => { + if('install' in exportedValue){ + return exportedValue.install(current.config || {}).then(() =>{ + logger.debug(`Installed plugin ${current.moduleId}.`); + }); + }else{ + logger.debug(`Loaded plugin ${current.moduleId}.`); + } + })); + } + + return Promise.all(toLoad); +} + export class Aurelia { constructor(loader, container, resources){ this.loader = loader || Loader.createDefaultLoader(); this.container = container || new Container(); this.resources = resources || new ResourceRegistry(); this.resourcesToLoad = []; + this.plugins = []; this.withInstance(Aurelia, this); this.withInstance(Loader, this.loader); @@ -34,8 +55,8 @@ export class Aurelia { return this; } - withPlugins(config, baseUrl){ - logger.error('withPlugins is not yet implemented'); + withPlugin(moduleId, config){ + this.plugins.push({moduleId:moduleId, config:config}); return this; } @@ -61,14 +82,14 @@ export class Aurelia { logger.error('You must configure Aurelia with a BindingLanguage implementation.'); } - //TODO: configure plugins - - return this.container.get(ResourceCoordinator) - .importResources(this.resourcesToLoad).then(resources => { - resources.forEach(x => x.register(this.resources)); - logger.info('Aurelia Started'); - return this; - }); + return loadPlugins(this.loader, this.plugins).then(() => { + return this.container.get(ResourceCoordinator) + .importResources(this.resourcesToLoad).then(resources => { + resources.forEach(x => x.register(this.resources)); + logger.info('Aurelia Started'); + return this; + }); + }); } setRoot(root, applicationHost){