diff --git a/lib/hooks/blueprints/onRoute.js b/lib/hooks/blueprints/onRoute.js index 45836c519..e71d0370b 100644 --- a/lib/hooks/blueprints/onRoute.js +++ b/lib/hooks/blueprints/onRoute.js @@ -44,6 +44,10 @@ module.exports = function(sails) { // On a match, merge leftover items in the target object into route options: options = _.merge(options, _.omit(target, 'blueprint')); + // Note: this (^) could be moved up into lib/router/bind.js, since its + // only pertinent for core options such as `skipAssets`. There would need + // to be changes in other hooks as well. + return bindBlueprintAction(path, target.blueprint, verb, options); } diff --git a/lib/hooks/controllers/to-interpret-route-syntax.js b/lib/hooks/controllers/to-interpret-route-syntax.js index 321f40d79..c4fab45cf 100644 --- a/lib/hooks/controllers/to-interpret-route-syntax.js +++ b/lib/hooks/controllers/to-interpret-route-syntax.js @@ -37,9 +37,11 @@ module.exports = function to(sails) { // `{ controller: 'UserController' }` if (_.isObject(target) && !_.isFunction(target) && !_.isArray(target)) { - // Merge target into `options` to get hold of relevant - // route options: + // Merge target into `options` to get hold of relevant route options: options = _.merge(options, target); + // Note: this (^) could be moved up into lib/router/bind.js, since its + // only pertinent for core options such as `skipAssets`. There would need + // to be changes in other hooks as well. // Support { controller: 'FooController' } notation (with or without `action`) if ( !_.isUndefined(target.controller) ) { diff --git a/lib/hooks/views/index.js b/lib/hooks/views/index.js index 6a081ce71..3e1159c78 100644 --- a/lib/hooks/views/index.js +++ b/lib/hooks/views/index.js @@ -2,7 +2,6 @@ * Module dependencies */ -var _ = require('lodash'); var configure = require('./configure'); var defaults = require('./defaults'); var onRoute = require('./onRoute'); @@ -25,7 +24,9 @@ module.exports = function (sails) { defaults: defaults, - configure: _.partial(configure, sails), + configure: function (){ + configure(sails); + }, render: render, @@ -66,7 +67,9 @@ module.exports = function (sails) { }); // Register `{view:'/foo'}` route target syntax. - sails.on('route:typeUnknown', _.partial(onRoute, sails)); + sails.on('route:typeUnknown', function (route){ + return onRoute(sails, route); + }); // Declare hook loaded when ejs layouts have been applied, // views have been inventoried, and view-serving middleware has been prepared diff --git a/lib/hooks/views/onRoute.js b/lib/hooks/views/onRoute.js index eeed8abe2..39528b121 100644 --- a/lib/hooks/views/onRoute.js +++ b/lib/hooks/views/onRoute.js @@ -28,6 +28,7 @@ var _ = require('lodash'); module.exports = function onRoute (sails, route) { + // Ignore unknown route syntax if ( !_.isPlainObject(route.target) || !_.isString(route.target.view) ) { // If it needs to be understood by another hook, the hook would have also received @@ -42,6 +43,13 @@ module.exports = function onRoute (sails, route) { } // Otherwise construct an action function which serves a view and then bind it to the route. else { + + // Merge target into `options` to get hold of relevant route options: + route.options = _.merge(route.options, route.target); + // Note: this (^) could be moved up into lib/router/bind.js, since its + // only pertinent for core options such as `skipAssets`. There would need + // to be changes in other hooks as well. + // Transform the view path into something Lodash _.get will understand (i.e. dots not slashes) var transformedViewAddress = route.target.view.replace(/\//g, '.'); var referenceInViewsHash = _.get(sails.views, transformedViewAddress); @@ -51,7 +59,7 @@ module.exports = function onRoute (sails, route) { if (referenceInViewsHash === true) { return sails.router.bind(route.path, function serveView(req, res) { return res.view(route.target.view); - }); + }, route.verb, route.options); } // Look for a relative `/index` view if the specified view // is in the views hash as a dictionary (i.e. indicating it is a directory) @@ -59,7 +67,7 @@ module.exports = function onRoute (sails, route) { var indexViewIdentity = path.join(route.target.view,'/index'); return sails.router.bind(route.path, function serveView(req, res) { return res.view(indexViewIdentity); - }); + }, route.verb, route.options); } // Otherwise, the specified view in this route target doesn't match a // known view in the project, so ignore the attempt and inform the user. diff --git a/lib/router/bind.js b/lib/router/bind.js index f6f81ca1b..5a3f1a348 100644 --- a/lib/router/bind.js +++ b/lib/router/bind.js @@ -137,7 +137,7 @@ function bindFunction(path, fn, verb, options) { var sails = this.sails; // Regex to check if a URL is an asset (something with a file extension) - var skipAssetsRegex = /^[^?]*\/[^?/]+\.[^?/]+(\?.*)?$/; + var skipAssetsRegex = /^[^?]*\/[^?\/]+\.[^?\/]+(\?.*)?$/; // Make sure (optional) options is a valid plain object ({}) options = _.isPlainObject(options) ? _.cloneDeep(options) : {}; diff --git a/test/helpers/test-spawning-sails-lift-child-process-in-cwd.js b/test/helpers/test-spawning-sails-lift-child-process-in-cwd.js index a60e70af7..73dc7ba3f 100644 --- a/test/helpers/test-spawning-sails-lift-child-process-in-cwd.js +++ b/test/helpers/test-spawning-sails-lift-child-process-in-cwd.js @@ -86,6 +86,14 @@ module.exports = function testSpawningSailsLiftChildProcessInCwd (opts){ cliArgs: [opts.pathToSailsCLI, 'lift'].concat(opts.liftCliArgs) }).execSync(); + // // For debugging, as needed: + // sailsLiftProc.stdout.on('data', function (data){ + // console.log('stdout:',''+data); + // }); + // sailsLiftProc.stderr.on('data', function (data){ + // console.log('stderr:',''+data); + // }); + // After N seconds, continue to the test. setTimeout(function (){ return done();