Author: hannes
Date: 2008-12-12 01:14:47 +0100 (Fri, 12 Dec 2008)
New Revision: 9415
Added:
helma-ng/trunk/apps/demo/actions.js
Modified:
helma-ng/trunk/apps/demo/config.js
helma-ng/trunk/apps/demo/main.js
helma-ng/trunk/apps/demo/webmodule.js
helma-ng/trunk/modules/helma/webapp.js
Log:
Simplify url mapping. Use config.urls only to route urls to modules, and do the intra-module stuff using _action suffix like Helma 1.
Details at http://dev.helma.org/trac/helma/changeset/9415
Copied: helma-ng/trunk/apps/demo/actions.js (from rev 9411, helma-ng/trunk/apps/demo/main.js)
Modified: helma-ng/trunk/apps/demo/config.js
===================================================================
--- helma-ng/trunk/apps/demo/config.js 2008-12-11 15:08:54 UTC (rev 9414)
+++ helma-ng/trunk/apps/demo/config.js 2008-12-12 00:14:47 UTC (rev 9415)
@@ -3,11 +3,8 @@
};
var urls = [
- [ /^$/, 'main.index' ],
- [ /^skins$/, 'main.skins' ],
- [ /^mount\/point/, 'webmodule.index' ],
- [ /^continuation/, 'main.continuation' ],
- [ /^logging/, 'main.logging' ],
+ [ /^mount\/point/, 'webmodule' ],
+ [ /^/, 'actions' ],
];
var middleware = [
Modified: helma-ng/trunk/apps/demo/main.js
===================================================================
--- helma-ng/trunk/apps/demo/main.js 2008-12-11 15:08:54 UTC (rev 9414)
+++ helma-ng/trunk/apps/demo/main.js 2008-12-12 00:14:47 UTC (rev 9415)
@@ -1,105 +1,6 @@
import('helma.webapp');
-import('helma.logging');
-import('webmodule', 'mount.point');
-
-var log = helma.logging.getLogger(__name__);
-
-// the main action is invoked for http://localhost:8080/
-function index(req, res) {
- res.render('skins/index.html', { title: 'Welcome to Helma NG' });
-}
-
-// demo for skins, macros, filters
-function skins(req, res) {
- var context = {
- title: 'Skin Demo',
- name: 'Luisa',
- names: ['Benni', 'Emma', 'Luca', 'Selma']
- };
- res.render('skins/skins.html', context);
-}
-
-// demo for log4j logging
-function logging(req, res) {
- if (req.data.info) {
- log.info("Hello world!");
- } else if (req.data.error) {
- try {
- foo.bar.moo;
- } catch (e) {
- log.error(e, e.rhinoException);
- }
- }
- res.render('skins/logging.html', { title: "Logging Demo" });
-}
-
-// demo for continuation support
-function continuation(req, res) {
-
- // local data - this is the data that is shared between resuming and suspension
- var data = {};
- var pages = ["start", "name", "favorite food", "favorite animal", "result"];
- // to have only one continuation per user just give the pages fixed ids
- // var pageIds = [0, 1, 2, 3, 4];
- // to have continuations created dynamically start with empty page ids
- var pageIds = [];
-
- // mark start of continuation code. We never step back earlier than this
- // otherwise local data would be re-initialized
- pageIds[0] = Continuation.startId(req);
- [req, res] = Continuation.markStart(req, res, pageIds[0]);
- // render intro page
- renderPage(0);
- // render first page
- renderPage(1)
- // render second page
- renderPage(2);
- // render third page
- renderPage(3);
- // render overview page
- if (!data.name) renderPage(1);
- renderPage(4);
-
- // the local function to do the actual work
- function renderPage(id) {
- var previous = pages[id - 1]
- if (req.isPost() && previous) {
- data[previous] = req.params[previous];
- }
- if (id < pages.length - 1) {
- pageIds[id + 1] = Continuation.nextId(req, pageIds[id + 1]);
- if (id < 1) {
- res.render('skins/continuation.html', {
- title: "Welcome",
- skin: "start",
- data: data,
- forward: Continuation.getUrl(req, pageIds[id + 1])
- });
- } else {
- res.render('skins/continuation.html', {
- title: "Question " + id,
- skin: "mask",
- input: pages[id],
- data: data,
- value: data[pages[id]],
- back: Continuation.getUrl(req, pageIds[id - 1]),
- forward: Continuation.getUrl(req, pageIds[id + 1])
- });
- }
- [req, res] = Continuation.nextPage(req, pageIds[id + 1]);
- } else {
- res.render('skins/continuation.html', {
- title: "Thanks!",
- skin: "result",
- data: data,
- back: Continuation.getUrl(req, pageIds[id - 1])
- });
- }
- }
-}
-
-// main method called to start application
+// main script to start application
if (__name__ == "__main__") {
helma.webapp.start();
}
Modified: helma-ng/trunk/apps/demo/webmodule.js
===================================================================
--- helma-ng/trunk/apps/demo/webmodule.js 2008-12-11 15:08:54 UTC (rev 9414)
+++ helma-ng/trunk/apps/demo/webmodule.js 2008-12-12 00:14:47 UTC (rev 9415)
@@ -1,6 +1,6 @@
// a simple web app/module
-function index(req, res) {
+function index_action(req, res) {
var context = {
title: 'Module Demo',
href: req.path
Modified: helma-ng/trunk/modules/helma/webapp.js
===================================================================
--- helma-ng/trunk/modules/helma/webapp.js 2008-12-11 15:08:54 UTC (rev 9414)
+++ helma-ng/trunk/modules/helma/webapp.js 2008-12-12 00:14:47 UTC (rev 9415)
@@ -16,7 +16,6 @@
var log = logging.getLogger(__name__);
-
/**
* Handler function called by the Helma servlet.
*
@@ -45,54 +44,65 @@
path = path.slice(1)
}
- function getRegExp(pattern) {
- if (pattern instanceof RegExp) {
- return pattern;
- } else if (typeof pattern == "string") {
- return new RegExp(pattern);
- } else {
+ function getPattern(spec) {
+ var pattern = spec[0];
+ if (typeof pattern == "string") {
+ pattern = spec[0] = new RegExp(pattern);
+ } else if (!(pattern instanceof RegExp)) {
throw Error("Pattern must be a regular expression or string");
}
+ return pattern;
}
+ function getModule(spec) {
+ var module = spec[1];
+ if (typeof module == "string") {
+ module = require(module);
+ } else if (!(module instanceof Object)) {
+ throw Error("Module must be a string or object");
+ }
+ return module;
+ }
+
+ function getAction(module, name) {
+ name = name || "index";
+ var action = module[name.replace(/\./g, "_") + "_action"];
+ if (typeof action == "function") {
+ return action;
+ }
+ return null;
+ }
+
try {
log.debug('resolving path ' + path);
if (config.urls instanceof Array) {
var urls = config.urls;
for (var i = 0; i < urls.length; i++) {
log.debug("checking url line: " + urls[i]);
- var match = getRegExp(urls[i][0]).exec(path);
+ var match = getPattern(urls[i]).exec(path);
log.debug("got match: " + match);
if (match != null) {
- var action = urls[i][1];
- log.debug("action: " + action);
- if (typeof action == "string") {
- log.debug("action is string");
- var dot = action.lastIndexOf('.');
- if (dot < 0) {
- throw Error('Action must be of form "module.function"');
- }
- var module = action.slice(0, dot);
- var func = action.slice(dot + 1);
- action = require(module)[func];
- if (log.debugEnabled) log.debug("resolved action: " + action);
- } else if (typeof action != "function") {
- throw Error('Action must either be a string or a function');
- }
+ var module = getModule(urls[i]);
+ log.debug("module: " + module);
+ // cut matching prefix from path
+ path = path.substring(match[0].length);
+ // remove leading and trailing slashes
+ path = path.replace(/^\/+|\/+$/g, "");
+ //split
+ path = path.split(/\/+/);
+ var action = getAction(module, path[0]);
if (typeof action == "function") {
- // got a match - add any regexp groups as additional action arguments
- var args = [req, res];
- var actionArgs = [];
- for (var j = 1; j < match.length; j++) {
- args.push(match[j]);
- actionArgs.push(match[j]);
- }
+ // add remaining path elements as additional action arguments
+ var actionArgs = path.slice(1)
+ .map(function (elem) decodeURIComponent(elem));
+ var args = [req, res].concat(actionArgs);
invokeMiddleware('onAction',
config.middleware,
[req, res, action, actionArgs]);
- action.apply(null, args);
+ action.apply(module, args);
return;
}
+ break;
}
}
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.