Skip to content

Commit

Permalink
updated formatting
Browse files Browse the repository at this point in the history
- %retab
- remove extra spacing
  • Loading branch information
tjwebb committed Aug 20, 2015
1 parent 5849abb commit 5049977
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 221 deletions.
173 changes: 78 additions & 95 deletions lib/hooks/views/actions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/**
* Module dependencies
*/

var _ = require('lodash');



/**
* Load views and generate view-serving middleware for each one
*
Expand All @@ -14,96 +8,85 @@ var _ = require('lodash');
* @param {Function} cb
* @api private
*/

module.exports = function detectAndPrepareViews (sails, hook, cb) {

glob(function (err, detectedViews) {
if (err) return cb(err);

// Save existence tree in `sails.views` for consumption later
sails.views = detectedViews || {};

// Generate view-serving middleware and stow it in `hook.middleware`
createMiddleware(detectedViews);

cb();
});


/**
* Generates view-serving middleware for each view
*/

function createMiddleware (detectedViews) {

// If there are any matching views which don't have an action
// create middleware to serve them
_.each(detectedViews, function (view, id) {

// Create middleware for a top-level view
if (view === true) {
sails.log.silly('Building action for view: ', id);
hook.middleware[id] = serveView(id);
}

// Create middleware for each subview
else {
hook.middleware[id] = {};
for (var subViewId in detectedViews[id]) {
sails.log.silly('Building action for view: ', id, '/', subViewId);
hook.middleware[id][subViewId] = serveView(id, subViewId);
}
}

}, hook);
}


/**
* Exits with a tree indicating which views exist
*/

function glob (cb) {

// Glob/stat views so we know whether they exist or not
sails.modules.statViews(cb);
}





/**
* Returns a middleware chain that remembers a view id and
* runs simple middleware to template and serve the view file.
* Used to serve views w/o controllers
*
* (This concatenation approach is crucial to allow policies to be bound)
*
* @param {[type]} viewId [description]
* @param {[type]} subViewId [description]
* @return {Array}
*/

function serveView (viewId, subViewId) {

// Save for use in closure
// (handle top-level and subview cases)
var viewExpression = viewId + (subViewId ? '/' + subViewId : '');

return [function rememberViewId(req, res, next) {

// Save reference for view in res.view() middleware
// (only needs to happen if subViewId is not set [top-level view])
if (viewId) {
req.options.view = viewExpression;
}

next();

}].concat(function serveView(req, res) {
res.view();
});
}
glob(function (err, detectedViews) {
if (err) return cb(err);

// Save existence tree in `sails.views` for consumption later
sails.views = detectedViews || {};

// Generate view-serving middleware and stow it in `hook.middleware`
createMiddleware(detectedViews);

cb();
});

/**
* Generates view-serving middleware for each view
*/
function createMiddleware (detectedViews) {

// If there are any matching views which don't have an action
// create middleware to serve them
_.each(detectedViews, function (view, id) {

// Create middleware for a top-level view
if (view === true) {
sails.log.silly('Building action for view: ', id);
hook.middleware[id] = serveView(id);
}

// Create middleware for each subview
else {
hook.middleware[id] = {};
for (var subViewId in detectedViews[id]) {
sails.log.silly('Building action for view: ', id, '/', subViewId);
hook.middleware[id][subViewId] = serveView(id, subViewId);
}
}

}, hook);
}

/**
* Exits with a tree indicating which views exist
*/
function glob (cb) {
// Glob/stat views so we know whether they exist or not
sails.modules.statViews(cb);
}

/**
* Returns a middleware chain that remembers a view id and
* runs simple middleware to template and serve the view file.
* Used to serve views w/o controllers
*
* (This concatenation approach is crucial to allow policies to be bound)
*
* @param {[type]} viewId [description]
* @param {[type]} subViewId [description]
* @return {Array}
*/
function serveView (viewId, subViewId) {

// Save for use in closure
// (handle top-level and subview cases)
var viewExpression = viewId + (subViewId ? '/' + subViewId : '');

return [function rememberViewId(req, res, next) {

// Save reference for view in res.view() middleware
// (only needs to happen if subViewId is not set [top-level view])
if (viewId) {
req.options.view = viewExpression;
}

next();

}].concat(function serveView(req, res) {
res.view();
});
}
};

34 changes: 11 additions & 23 deletions lib/hooks/views/configure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
/**
* Module dependencies
*/

var _ = require('lodash')
, path = require('path')
, util = require('util')
, consolidate = require('consolidate');


var _ = require('lodash');
var path = require('path');
var util = require('util');
var consolidate = require('consolidate');

/**
* Marshal relevant parts of sails global configuration,
* issue deprecation notices, etc.
*
* @param {Sails} sails
*/

module.exports = function configure ( sails ) {

if (sails.config.viewEngine) {
Expand All @@ -25,10 +18,10 @@ module.exports = function configure ( sails ) {
}

// Normalize view engine config and allow defining a custom extension
if (typeof sails.config.views.engine === 'string') {
if (_.isString(sails.config.views.engine)) {
var viewExt = sails.config.views.extension || sails.config.views.engine;
sails.config.views.engine = {
name: sails.config.views.engine,
name: sails.config.views.engine,
ext: viewExt
};
}
Expand All @@ -55,10 +48,7 @@ module.exports = function configure ( sails ) {
var appDependenciesPath;
var fn;

appDependenciesPath = path.join(
sails.config.appPath,
'node_modules'
);
appDependenciesPath = path.join(sails.config.appPath, 'node_modules');

try {

Expand Down Expand Up @@ -87,7 +77,6 @@ module.exports = function configure ( sails ) {
sails.log.silly('Configured view engine, `' + engineName + '`');
}


// Let user know that a leading . is not required in the viewEngine option and then fix it
if (sails.config.views.engine.ext[0] === '.') {
sails.log.warn('A leading `.` is not required in the views.engine option. Removing it for you...');
Expand Down Expand Up @@ -126,8 +115,8 @@ function getEngine(appDependenciesPath,engineName,moduleName) {
var fn = consolidate[engineName];

// If we haven't cached the engine module in consolidate's requires cache, do it now.
if(!consolidate.requires[moduleName]){
try{
if(!consolidate.requires[moduleName]) {
try {
//ensure the engine is required relative to the path of our app
consolidate.requires[moduleName] = require(appDependenciesPath+'/' + moduleName);
// If the engine name and module names are different, cache the engine under both
Expand All @@ -137,11 +126,10 @@ function getEngine(appDependenciesPath,engineName,moduleName) {
if (engineName != moduleName) {
consolidate.requires[engineName] = consolidate.requires[moduleName];
}
}catch(e){
console.log(['Could not find module:',moduleName,"in path:",appDependenciesPath].join(" "));
} catch(e) {
sails.log.info('Could not find module:', moduleName , 'in path:', appDependenciesPath);
}
}

return fn;

}
Loading

0 comments on commit 5049977

Please sign in to comment.