controller.js
* @module greppy/http/mvc/controller
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var BaseController = function()
// Load all Greppy controller helpers dynamically
greppy.helper.list().forEach(function(itm) {
// load only controller helpers
if (!/^controller\./.test(itm)) {
self[helper.toCamelCase()] = greppy.helper.get(itm);
* @param {Object} app - The application object
* @param {Object} server - Server object
* @param {Function} callback - Function to call on finish
BaseController.prototype.configure = function(app, server, callback)
* Build a view path by a given file.
* @param {String} file - Name of the view file
BaseController.prototype.view = function(file)
return path.normalize(this.viewPath + '/' + file);
* @param {String} action - Name of the action to link to
* @param {Object} params - Parameters object
* @param {String} [absoluteUrl] - If given, it will be prefixed
BaseController.prototype.link = function(action, params, absoluteUrl)
if (!this.actions.hasOwnProperty(action)) {
'Action "' + action + '" is not registered for the controller'
var link = this.basePath + this.actions[action].path;
// Replace all parameters which are given by the user
Object.keys(params).forEach(function(param) {
link = link.replace(new RegExp(':' + param + '[\?]?', 'ig'), params[param]);
// Remove any optional parameters which where not specified
link = link.replace(new RegExp(':.*\\?', 'ig'), '');
// Replace multiple slashes to a single one: /// -> /
link = link.replace(/[/]+/gi, '/');
var unreplaced = link.split('/').filter(function(itm) {
if (0 !== unreplaced.length) {
'Failed to fully resolve the action link for "' + action + '". ' +
'Unresolved parameters: ' + unreplaced.join(', ') +
'\nGiven parameters:\n' + JSON.stringify(params, null, ' ')
return (absoluteUrl) ? absoluteUrl + link : link;