Skip to content

Commit

Permalink
Temporary solutions for results handler config option. Works, but con…
Browse files Browse the repository at this point in the history
…ceptually wrong.
  • Loading branch information
tobie committed Sep 22, 2010
1 parent e249e9e commit ba65c33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lib/config.js
Expand Up @@ -30,7 +30,7 @@ function createConfig(options) {
// is stored as a full path (defaults to current working dir).
// Querying config props always returns a full path.

var self = {}, _o = {}, _keys, props;
var self = {}, _o = {}, _keys, props, _resultsHandlerFunction;

function _proxy(key) {
return {
Expand Down Expand Up @@ -146,7 +146,7 @@ function createConfig(options) {
var initValue = handler;

if (typeof handler === 'function') {
_o.resultsHandler = handler;
_resultsHandlerFunction = handler;
return;
}

Expand All @@ -156,20 +156,24 @@ function createConfig(options) {
try {
// First, try loading the module from the resultHandler directory.
handler = require('./resultsHandler/' + initValue);
// if that worked, save the reference, it's the correct one.
_o.resultsHandler = initValue;
} catch(e) {
try {
// Might be an absolute path.
handler = require(initValue);
// if so, save it.
_o.resultsHandler = initValue;
} catch (e) {
throw new Error('Cannot find module "' + initValue + '".');
}
}
}

// If it's an object, it must have a handleResults method.
// If it's an object, look for its handleResults method.
if (typeof handler === 'object') {
if (typeof handler.handleResults === 'function') {
_o.resultsHandler = function() {
_resultsHandlerFunction = function() {
return handler.handleResults.apply(handler, arguments);
};
} else {
Expand All @@ -183,7 +187,8 @@ function createConfig(options) {
};

self.handleResults = handleResults;
function handleResults(options) {
function handleResults(results) {
return _resultsHandlerFunction(results);
}

self.getTemplatePath = getTemplatePath;
Expand Down
3 changes: 1 addition & 2 deletions lib/server.js
Expand Up @@ -11,11 +11,10 @@ function createServer(config) {
server.set('views', path.join(__dirname, '..', 'views'));
server.register('.mustache', require('./template'));

var resultsHandler = config.resultsHandler;
server.post('/results', function(req, res) {
var results = _createResults(req);
res.render('results.mustache', { locals: results });
resultsHandler(results);
config.handleResults(results);
});

server.listen(config.port);
Expand Down

0 comments on commit ba65c33

Please sign in to comment.