Skip to content

Commit

Permalink
Update functionality of globals config
Browse files Browse the repository at this point in the history
* The implicit default is `false` -- no globals at all
* If `sails.config.globals` is not `false`, it must be a dictionary such that:
  - If `sails.config.globals._` is not `false`, it must be an object
  - If `sails.config.globals.async` is not `false`, it must be an object
  - `sails.config.globals.models` must be a boolean
  - `sails.config.globals.sails` must be a boolean
* `sails.config.globals.services` still defaults to `true` if ``sails.config.globals` is a dictionary, because otherwise we have to talk about it explicitly and we're trying to phase services out.
  • Loading branch information
sgress454 committed Nov 28, 2016
1 parent 1b970b6 commit 44c6d9b
Show file tree
Hide file tree
Showing 6 changed files with 760 additions and 33 deletions.
47 changes: 39 additions & 8 deletions lib/app/private/exposeGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var _ = require('@sailshq/lodash');
var flaverr = require('flaverr');
var async = require('async');


Expand All @@ -18,30 +19,60 @@ var async = require('async');
module.exports = function exposeGlobals() {
var sails = this;

// Implicit default for globals is `false`.
// Note that this may not be what the default `config/globals.js`
// settings are.
if (_.isUndefined(sails.config.globals)) {
sails.config.globals = false;
return;
}

// Globals explicitly disabled
// If globals config is provided, it must be either `false` or a dictionary.
else if (sails.config.globals !== false && (!_.isObject(sails.config.globals) || _.isArray(sails.config.globals) || _.isFunction(sails.config.globals))) {
throw flaverr('E_BAD_GLOBAL_CONFIG', new Error('If `sails.config.globals` is defined, it must either be a dictionary or `false`'));
}

// Globals explicitly disabled.
if (sails.config.globals === false) {
sails.log.verbose('No global variables will be exposed.');
return;
}

sails.log.verbose('Exposing global variables... (you can customize/disable this by modifying the properties in `sails.config.globals`. Set it to `false` to disable all globals.)');

sails.config.globals = sails.config.globals || {};

// Provide global access (if allowed in config)
// `sails.config.globals._` must be false or an object.
// (it's probably a function with lots of extra properties, but to future-proof, we'll allow any type of object)
if (sails.config.globals._ !== false) {
global['_'] = _;
if (!_.isObject(sails.config.globals._)) {
throw flaverr('E_BAD_GLOBAL_CONFIG', new Error('If `sails.config.globals._` is defined, it must be either `false` or an object.'));
}
global['_'] = sails.config.globals._;
}
// `sails.config.globals.async` must be false or an object.
// (it's probably a plain object aka dictionary, but to future-proof, we'll allow any type of object)
if (sails.config.globals.async !== false) {
global['async'] = async;
if (!_.isObject(sails.config.globals.async)) {
throw flaverr('E_BAD_GLOBAL_CONFIG', new Error('If `sails.config.globals.async` is defined, it must be either `false` or an object.'));
}
global['async'] = sails.config.globals.async;
}

// `sails.config.globals.sails` must be a boolean
if (sails.config.globals.sails !== false) {
if (sails.config.globals.sails !== true) {
throw flaverr('E_BAD_GLOBAL_CONFIG', new Error('If `sails.config.globals` is defined, then `sails.config.globals.sails` must be a boolean.'));
}
global['sails'] = sails;
}

// `services` hook takes care of globalizing services (if enabled)
// `sails.config.globals.models` must be a boolean.
// `orm` hook takes care of actually globalizing models and adapters (if enabled)
if (sails.config.globals.models !== false && sails.config.globals.models !== true) {
throw flaverr('E_BAD_GLOBAL_CONFIG', new Error('If `sails.config.globals` is defined, then `sails.config.globals.models` must be a boolean.'));
}

// `orm` hook takes care of globalizing models and adapters (if enabled)
// `services` hook takes care of globalizing services (if enabled)
// It does this by default for now, so that we don't have to document configuring
// services, which we're trying to phase out in favor of helpers.

};
2 changes: 1 addition & 1 deletion test/hooks/views/res.render.i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('sails.hooks.views.render() with i18n', function (){
before(function (done) {
app = new Sails()
.load({
globals: true,
globals: { sails: true, models: false, _: false, async: false, services: false } ,
loadHooks: [
'moduleloader',
'userconfig',
Expand Down
Loading

0 comments on commit 44c6d9b

Please sign in to comment.