If you want to define a new route, you use a sinatra-like way.
But instead of calling a global get/post-function, you construct a new object,
which implements the Controller interface and give it the path (can be
a String or a RegExp) as first argument.
For example:
new Controller("ping/hello/world", {
"execute": function(params, context) {
return function(cb) {
cb("pong!");
}
}
});
This gives you the power to extend the controller and reuse it in your application to keep your code DRY.
This is a sample implementation for a SyncController and its usage:
// Definition of the SyncController:
SyncController = function(path, sync_function) {
var options = [
path, {
execute: function() {
var args = arguments;
return function(cb) {
cb(sync_function.apply(this, args));
};
}
}
];
Controller.prototype.constructor.apply(this, options);
};
extend(true, SyncController.prototype, Controller.prototype);
// Usage:
new SyncController("ping/hello/world", function(params, context) {
return "pong!";
});
As you can see, the SyncController is a controller, which does not need the
evented way and wants to return the value.
There are plenty other use cases for this. For instance an ApiController,
which receives SOAP or just returns a value which get's wrapped into a
SOAP-Reply or a JSON-String.
You are also able to use RegExp objects as path.
new SyncController(/^user_comments/\d+/\d+$/, function(params, context) {
var user_id = params[1];
var page = params[2];
return "Hello user #" + user_id + ". We have no page " + page;
});
The SyncController is already part of Spludo. And it's implemented exactly this
way.