Skip to content

Commit

Permalink
feat(bootstrapper): new startup logic
Browse files Browse the repository at this point in the history
This is a BREAKING CHANGE. aurelia-main is no longer supported. Instead
we use aurelia-app for both cases. If not value is provided for the
attr, then we use the default configuration and load app.js as the root
view model. If a value is provided we load that module which serves as
the configuration module. We’ve also added some new apis to aurelia.use
in order to streamline manual configuration.
  • Loading branch information
EisenbergEffect committed Mar 24, 2015
1 parent 13b0416 commit 373bf74
Showing 1 changed file with 58 additions and 47 deletions.
105 changes: 58 additions & 47 deletions src/index.js
Expand Up @@ -57,7 +57,7 @@ function ensureLoader(){
});
}

return Promise.resolve();
return Promise.resolve();
}

function preparePlatform(){
Expand Down Expand Up @@ -107,6 +107,8 @@ function preparePlatform(){
});
}

var installedDevelopmentLogging = false;

function configureAurelia(aurelia){
return System.normalize('aurelia-bootstrapper').then(function(bName){
var toLoad = [];
Expand Down Expand Up @@ -144,67 +146,76 @@ function configureAurelia(aurelia){
};
}));

aurelia.use.standardConfiguration = function(){
aurelia.use
.defaultBindingLanguage()
.defaultResources()
.router()
.eventAggregator();
return this;
};

aurelia.use.developmentLogging = function(){
if(!installedDevelopmentLogging){
installedDevelopmentLogging = true;
LogManager.addAppender(new ConsoleAppender());
LogManager.setLevel(LogManager.levels.debug);
}
return this;
}

return Promise.all(toLoad);
});
}

function handleMain(mainHost){
var mainModuleId = mainHost.getAttribute('aurelia-main') || 'main',
loader = new window.AureliaLoader();

return loader.loadModule(mainModuleId)
.then(m => {
var aurelia = new Aurelia(loader);
return configureAurelia(aurelia).then(() => { return m.configure(aurelia); });
}).catch(e => {
setTimeout(function(){ throw e; }, 0);
});
function runningLocally(){
return window.location.protocol !== 'http' && window.location.protocol !== 'https';
}

function handleApp(appHost){
var appModuleId = appHost.getAttribute('aurelia-app') || './app',
aurelia = new Aurelia();

return configureAurelia(aurelia).then(() => {
aurelia.use
.defaultBindingLanguage()
.defaultResources()
.router()
.eventAggregator();

if(appHost.hasAttribute('es5')){
aurelia.use.es5();
} else if(appHost.hasAttribute('atscript')){
aurelia.use.atscript();
}
var configModuleId = appHost.getAttribute('aurelia-app'),
aurelia, loader;

return aurelia.start().then(a => { return a.setRoot(appModuleId, appHost); });
}).catch(e => {
setTimeout(function(){ throw e; }, 0);
});
}
if(configModuleId){
loader = new window.AureliaLoader();

function runningLocally(){
return window.location.protocol !== 'http' &&
window.location.protocol !== 'https';
return loader.loadModule(configModuleId)
.then(m => {
aurelia = new Aurelia(loader);
return configureAurelia(aurelia).then(() => { return m.configure(aurelia); });
}).catch(e => {
setTimeout(function(){ throw e; }, 0);
});
}else{
aurelia = new Aurelia();

return configureAurelia(aurelia).then(() => {
if(runningLocally()){
aurelia.use.developmentLogging();
}

aurelia.use.standardConfiguration();

if(appHost.hasAttribute('es5')){
aurelia.use.es5();
} else if(appHost.hasAttribute('atscript')){
aurelia.use.atscript();
}

return aurelia.start().then(a => { return a.setRoot(undefined, appHost); });
}).catch(e => {
setTimeout(function(){ throw e; }, 0);
});
}
}

function run() {
return ready(window).then(function (doc) {
var mainHost = doc.querySelectorAll("[aurelia-main]"),
appHost = doc.querySelectorAll("[aurelia-app]"),
i, ii;

if (appHost.length && !mainHost.length && runningLocally()) {
LogManager.addAppender(new ConsoleAppender());
LogManager.setLevel(LogManager.levels.debug);
}
return ready(window).then(doc => {
var appHost = doc.querySelectorAll("[aurelia-app]");

return ensureLoader().then(() => {
return preparePlatform().then(function(){
for (i = 0, ii = mainHost.length; i < ii; ++i) {
handleMain(mainHost[i]);
}
var i, ii;

for (i = 0, ii = appHost.length; i < ii; ++i) {
handleApp(appHost[i]);
Expand Down

0 comments on commit 373bf74

Please sign in to comment.