diff --git a/lib/hooks/controllers/onRoute.js b/lib/hooks/controllers/onRoute.js index b5036cd3c..59841f862 100644 --- a/lib/hooks/controllers/onRoute.js +++ b/lib/hooks/controllers/onRoute.js @@ -17,9 +17,14 @@ module.exports = function (sails) { * interpretRouteSyntax * * "Teach" router to understand references to controllers. + * This is the event handler for the 'route:typeUnknown' emitted on `sails`. + * + * @param {Dictionary} route + * @property {String} verb + * @property {String} path + * @property {Dictionary} target + * @property {Dictionary} options * - * @param {[type]} route [description] - * @return {[type]} [description] * @api private */ return function interpretRouteSyntax (route) { @@ -29,6 +34,8 @@ module.exports = function (sails) { var options = route.options; + // Support various dictionary target notations, e.g.: + // `{ controller: 'UserController' }` if (_.isObject(target) && !_.isFunction(target) && !_.isArray(target)) { // Merge target into `options` to get hold of relevant @@ -36,19 +43,18 @@ module.exports = function (sails) { options = _.merge(options, target); // Support { controller: 'FooController' } notation - if (!_.isUndefined(target.controller)) { + if ( !_.isUndefined(target.controller) ) { return bindController(path, target, verb, options); } // Support resourceful sub-mappings for verbless routes // e.g. '/someRoute': { post: 'FooController.bar', get: '...', /* ... */ } - // If verb was manually specified in route (e.g. `get /someRoute`), ignore the sub-mappings + // If verb was manually specified in route address (e.g. `get /someRoute`), ignore the sub-mappings. if ( !options.detectedVerb ) { if ( target.get ) { sails.router.bind (path, target['get'],'get', options); } if ( target.post ) { sails.router.bind (path, target['post'],'post', options); } if ( target.put ) { sails.router.bind (path, target['put'],'put', options); } if ( target['delete'] ) { sails.router.bind (path, target['delete'],'delete', options); } - // TODO: if there is a legitimate use case for it, add other HTTP verbs here for completeness. } @@ -56,7 +62,7 @@ module.exports = function (sails) { // '/someRoute': { action: 'find', model: 'foo' } // // (useful for explicitly routing a URL to a blueprint action) - if ( !_.isUndefined(target.action) ) { + else if ( !_.isUndefined(target.action) ) { // Merge target def. into route options: options.action = target.action; @@ -66,7 +72,7 @@ module.exports = function (sails) { } // Support string ('FooController.bar') notation - if (_.isString(target)) { + else if (_.isString(target)) { // Handle dot notation var parsedTarget = target.match(/^([^.]+)\.?([^.]*)?$/);