Skip to content

Commit

Permalink
fix(aurelia): update to load global resources through new resource pi…
Browse files Browse the repository at this point in the history
…peline

BREAKING CHANGE The method withResources has been renamed to
globalizeResources. You must pass relative module ids to this method.
We have also added a new method named renameGlobalResource which allows
you to rename globally imported resources.
  • Loading branch information
EisenbergEffect committed Mar 8, 2015
1 parent fa9cd3a commit bdbca55
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -13,6 +13,7 @@ This library is part of the [Aurelia](http://www.aurelia.io/) platform and conta
* [aurelia-loader](https://github.com/aurelia/loader)
* [aurelia-task-queue](https://github.com/aurelia/task-queue)
* [aurelia-logging](https://github.com/aurelia/logging)
* [aurelia-path](https://github.com/aurelia/path)

## Used By

Expand Down
1 change: 1 addition & 0 deletions config.js
Expand Up @@ -14,6 +14,7 @@ System.config({
"aurelia-loader": "github:aurelia/loader@0.3.5",
"aurelia-logging": "github:aurelia/logging@0.2.5",
"aurelia-metadata": "github:aurelia/metadata@0.3.3",
"aurelia-path": "github:aurelia/path@0.4.5",
"aurelia-task-queue": "github:aurelia/task-queue@0.2.5",
"aurelia-templating": "github:aurelia/templating@0.8.14",
"github:aurelia/binding@0.3.7": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -30,6 +30,7 @@
"aurelia-loader": "github:aurelia/loader@^0.3.5",
"aurelia-logging": "github:aurelia/logging@^0.2.5",
"aurelia-metadata": "github:aurelia/metadata@^0.3.3",
"aurelia-path": "github:aurelia/path@^0.4.5",
"aurelia-task-queue": "github:aurelia/task-queue@^0.2.5",
"aurelia-templating": "github:aurelia/templating@^0.8.14"
}
Expand Down
69 changes: 44 additions & 25 deletions src/aurelia.js
@@ -1,8 +1,16 @@
import * as LogManager from 'aurelia-logging';
import {Container} from 'aurelia-dependency-injection';
import {Loader} from 'aurelia-loader';
import {BindingLanguage, ResourceCoordinator, ViewSlot, ResourceRegistry, CompositionEngine, Animator} from 'aurelia-templating';
import {join,relativeToFile} from 'aurelia-path';
import {Plugins} from './plugins';
import {
BindingLanguage,
ResourceCoordinator,
ViewSlot,
ResourceRegistry,
CompositionEngine,
Animator
} from 'aurelia-templating';

var logger = LogManager.getLogger('aurelia'),
slice = Array.prototype.slice;
Expand Down Expand Up @@ -37,20 +45,15 @@ function preventActionlessFormSubmit() {

function loadResources(container, resourcesToLoad, appResources){
var resourceCoordinator = container.get(ResourceCoordinator),
current;
importIds = Object.keys(resourcesToLoad),
names = new Array(importIds.length),
i, ii;

function next(){
if(current = resourcesToLoad.shift()){
return resourceCoordinator.importResourcesFromModuleIds(current, current.resourceManifestUrl).then(resources => {
resources.forEach(x => x.register(appResources));
return next();
});
}

return Promise.resolve();
for(i = 0, ii = importIds.length; i < ii; ++i){
names[i] = resourcesToLoad[importIds[i]];
}

return next();
return resourceCoordinator.importViewResources(importIds, names, appResources);
}

/**
Expand All @@ -68,7 +71,7 @@ export class Aurelia {
this.container = container || new Container();
this.resources = resources || new ResourceRegistry();
this.use = new Plugins(this);
this.resourcesToLoad = [];
this.resourcesToLoad = {};

this.withInstance(Aurelia, this);
this.withInstance(Loader, this.loader);
Expand Down Expand Up @@ -102,16 +105,37 @@ export class Aurelia {
}

/**
* Adds a resource to be imported into the Aurelia framework.
* Adds globally available view resources to be imported into the Aurelia framework.
*
* @method globalizeResources
* @param {Object|Array} resources The relative module id to the resource. (Relative to the plugin's installer.)
* @return {Aurelia} Returns the current Aurelia instance.
*/
globalizeResources(resources){
var toAdd = Array.isArray(resources) ? resources : arguments,
i, ii, pluginPath = this.currentPluginId, path,
internalPlugin = pluginPath.startsWith('./');

for(i = 0, ii = toAdd.length; i < ii; ++i){
path = internalPlugin
? relativeToFile(toAdd[i], pluginPath)
: join(pluginPath, toAdd[i]);
this.resourcesToLoad[path] = this.resourcesToLoad[path];
}

return this;
}

/**
* Renames a global resource that was imported.
*
* @method withResources
* @param {Object|Array} resources The constructor function(s) to use when the dependency needs to be instantiated.
* @method renameGlobalResource
* @param {String} resourcePath The path to the resource.
* @param {String} newName The new name.
* @return {Aurelia} Returns the current Aurelia instance.
*/
withResources(resources){
var toAdd = Array.isArray(resources) ? resources : slice.call(arguments);
toAdd.resourceManifestUrl = this.currentPluginId;
this.resourcesToLoad.push(toAdd);
renameGlobalResource(resourcePath, newName){
this.resourcesToLoad[resourcePath] = newName;
return this;
}

Expand All @@ -131,9 +155,6 @@ export class Aurelia {

preventActionlessFormSubmit();

var resourcesToLoad = this.resourcesToLoad;
this.resourcesToLoad = [];

return this.use._process().then(() => {
if(!this.container.hasHandler(BindingLanguage)){
var message = 'You must configure Aurelia with a BindingLanguage implementation.';
Expand All @@ -145,8 +166,6 @@ export class Aurelia {
this.withInstance(Animator, new Animator());
}

this.resourcesToLoad = this.resourcesToLoad.concat(resourcesToLoad);

return loadResources(this.container, this.resourcesToLoad, this.resources).then(() => {
logger.info('Aurelia Started');
var evt = new window.CustomEvent('aurelia-started', { bubbles: true, cancelable: true });
Expand Down

0 comments on commit bdbca55

Please sign in to comment.