DarkChipolata
DarkChipolata

Reputation: 965

Define variables out of a file

I just discovered Sails.js, a MVC framework for Node.js. A controller is a file who export an object associating a method name to a function, il looks like this :

module.exports = {
   method : function(req, res) {
  /* ... */
  },
}

However, in these functions, we can use models, defined in other files, used like this in the functions :

method: function(req, res) {
    Message.create(/* ... */);
}

The Message variable isn't defined in the file, no require() in the top of the file, so how it can be done ?

Upvotes: 1

Views: 66

Answers (1)

srquinn
srquinn

Reputation: 10481

Sails.js has a lot of "automagic" going on under the covers. By default, sails will expose models, adapters and services to the global context of your controller module:

https://github.com/balderdashy/sails/blob/master/lib/configuration/defaults.js#L57 https://github.com/balderdashy/sails/blob/master/lib/app/exposeGlobals.js

If you don't like this (and you shouldn't because we all know globals are evil), then you can turn it off through configuration, and manually require the models from the correct directory.

Upvotes: 1

Related Questions