Global variables and file load order

I am attempting to utilize aldeed’s SimpleSchema and Collection2 packages for Mongo schema validation. I have a number of files in my server/ directory pertaining to various collections. I have a global Schema object:

Schemas = {};

And then Schemas.<collection> = new SimpleSchema inside each file. Now I have noticed that my application fails with "Schemas is undefined" errors unless my global declaration is placed in a file that alphabetically precedes my first Schemas.<collection> instantiation. Currently I’m handling this by creating a server/a.js file that contains just my Schemas = {}; but that seems really idiotic. I’ve attempted to use Meteor.startup({ Schemas = {}; }); but that fails as well.

So what is the correct way to handle declaring globals so that they are not dependent on arbitrary file load orders?

if (typeOf(Schemas) !== undefined ) { Schemas = {}; } ?

I am too lazy to check if it is valid JS, it is easier in coffeescript :smiley:

have you tried putting the declaration in a lib folder?

see file load order

There are several load ordering rules. They are applied sequentially to all
applicable files in the application, in the priority given below:

HTML template files are always loaded before everything elseFiles beginning with main. are loaded lastFiles inside any lib/ directory are loaded nextFiles with deeper paths are loaded nextFiles are then loaded in alphabetical order of the entire path

I have not. I assume that would work. I just find it odd that Meteor makes such a big deal out of scoping variables to a file, gives you a way to create cross-file global scoped variables, and then hinges whether or not that global will be recognized on file load order.

I’ll just throw a file in the lib directory containing that one line and never look at it again.

Create a core package for them. The package is loaded before all you app code.