1
0
Fork 0

fixed app context

This commit is contained in:
Frank Celler 2015-01-18 20:58:00 +01:00
parent bd2a698ab6
commit b80708ebfa
1 changed files with 17 additions and 29 deletions

View File

@ -108,7 +108,7 @@ var ALL_METHODS = [ "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" ]
function notImplementedFunction (route, message) { function notImplementedFunction (route, message) {
'use strict'; 'use strict';
message += "\nThis error was triggered by the following route " + JSON.stringify(route); message += "\nThis error was triggered by the following route '" + route.name + "'";
console.errorLines("%s", message); console.errorLines("%s", message);
@ -410,7 +410,7 @@ function lookupCallbackAction (route, action, context) {
// ............................................................................. // .............................................................................
if (action.hasOwnProperty('callback')) { if (action.hasOwnProperty('callback')) {
return lookupCallbackActionCallback(route, action, context.appModule); return lookupCallbackActionCallback(route, action, context.module);
} }
// ............................................................................. // .............................................................................
@ -718,7 +718,7 @@ function defineRoutePart (storage, route, parts, pos, constraint, callback) {
} }
if (pos + 1 < parts.length) { if (pos + 1 < parts.length) {
console.error("cannot define prefix match within url, ignoring route"); console.error("cannot define prefix match within url, ignoring route '%s'", route.name);
} }
else { else {
var subprefix = storage.prefix; var subprefix = storage.prefix;
@ -794,14 +794,14 @@ function installRoute (storage, route, urlPrefix, context) {
var url = lookupUrl(urlPrefix, route.url); var url = lookupUrl(urlPrefix, route.url);
if (url === null) { if (url === null) {
console.error("route '%s' has an unkown url, ignoring", JSON.stringify(route)); console.error("route '%s' has an unkown url, ignoring '%s'", route.name);
return; return;
} }
var callback = lookupCallback(route, context); var callback = lookupCallback(route, context);
if (callback === null) { if (callback === null) {
console.error("route '%s' has an unknown callback, ignoring", JSON.stringify(route)); console.error("route '%s' has an unknown callback, ignoring '%s'", route.name);
return; return;
} }
@ -812,7 +812,7 @@ function installRoute (storage, route, urlPrefix, context) {
/// @brief creates additional routing information /// @brief creates additional routing information
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
function analyseRoute (storage, routes) { function analyseRoutes (storage, routes) {
'use strict'; 'use strict';
var j; var j;
@ -820,26 +820,18 @@ function analyseRoute (storage, routes) {
var urlPrefix = routes.urlPrefix || ""; var urlPrefix = routes.urlPrefix || "";
// create the application context // use normal root module or app context
var appModule = module.root; var appContext;
var appContext = {
collectionPrefix: ""
};
if (routes.hasOwnProperty('appContext')) { if (routes.hasOwnProperty('appContext')) {
appContext = routes.appContext; appContext = routes.appContext;
var appId = appContext.appId;
var app = module.createApp(appId);
if (app === null) {
throw new Error("unknown application '" + appId + "'");
}
appModule = app.createAppModule(appContext);
} }
else {
appContext = {
module: module.root
};
}
// install the routes // install the routes
var keys = [ 'routes', 'middleware' ]; var keys = [ 'routes', 'middleware' ];
@ -850,12 +842,10 @@ function analyseRoute (storage, routes) {
var r = routes[key]; var r = routes[key];
for (i = 0; i < r.length; ++i) { for (i = 0; i < r.length; ++i) {
var context = { appModule: appModule };
installRoute(storage[key], installRoute(storage[key],
r[i], r[i],
urlPrefix, urlPrefix,
context); appContext);
} }
} }
} }
@ -884,16 +874,14 @@ function routingTree (routes) {
try { try {
if (route.hasOwnProperty('routes') || route.hasOwnProperty('middleware')) { if (route.hasOwnProperty('routes') || route.hasOwnProperty('middleware')) {
analyseRoute(storage, route); analyseRoutes(storage, route);
} }
else { else {
installRoute(storage.routes, route, "", {}); installRoute(storage.routes, route, "", {});
} }
} }
catch (err) { catch (err) {
console.errorLines("cannot install route '%s': %s", console.errorLines("cannot install route '%s': %s", route.name, String(err.stack || err));
route.name,
String(err.stack || err));
} }
} }