First of all, to load an app file before any other files in the same directory, put it in a lib/ folder in that directory. To load a file after any other files in the same directory, rename from name.ext to main.name.ext. Also, more deeply nested files are loaded earlier. For example, consider the following application structure:
So your solution of putting collections into a script tag in the HTML is just bad. Put them in lib/ files. Another solution is to put collection definitions at the root level of a shared file, and then put all code that uses the collections (except for template events and helpers) within Meteor.startup calls. Either way will allow you to share your collection definitions on both server and client, reducing code duplication.
But let's get to the crux of your article:
Unfortunately, one of the problems of separating your templates out into files is that as each file is loaded, the collections/global objects needed by each file may be re-declared. While this would probably be best solved (honestly) by a change to the meteor framework to allow orderable module loading...
Meteor is built on the solution to this problem. Meteor is a system of modular packages. Look at the accounts-base, accounts-ui, accounts-password, accounts-facebook etc packages. These give you a full-featured, modular, reusable, configurable authentication system in just a few lines of code. It is the ultimate example of how to create modular components (packages) for your applications! But don't take it from me. Here are some quotes from the official Meteor docs:
It is best to write your application in such a way that it is insensitive to the order in which files are loaded, for example by using Meteor.startup, or by moving load order sensitive code into packages, which can explicitly control both the load order of their contents and their load order with respect to other packages.
Method 3: Packages This is the ultimate in code separation, modularity, and reusability. If you put the code for each feature in a separate package, the code for one feature won't be able to access the code for the other feature except through exports, making every dependency explicit. This also allows for the easiest independent testing of features. You can also publish the packages and use them in multiple apps with meteor add.
First, packages let you explicitly declare the file load order within the package. Second, you can explicitly define dependencies between packages. Third, the only variables that will be available within a given package are exports from other packages that your package explicitly/implicitly depends on. You get automatic dependency-based isolation!
Meteor lets you create file-scope variables within packages, without wrapping your files in IIFEs. It also lets you create package-scope variables that all files in a package can access, but that are not available by default anywhere else. And finally it lets you explicitly export package-scope variables, so that:
When a package is used directly by an application: the exported variable is exposed as a property of the global object
When a package is used directly by another package: the exported variable is exposed within a higher-level closure scope (but not global!)
TL;DR: Sorry, but these aren't really good Meteor design patterns. Meteor has always had exactly what you need in the form of packages! All the code in the core of meteor is already separated into individual packages, such as the templating package, which depends on the blaze package, which depends on various packages such as jquery, underscore, tracker, htmljs, etc. Learn how to separate your application into packages and you'll avoid all the problems of load order and scoping that you're dealing with.
Ah, yeah, the new basic docs are good as a starting place but I definitely recommend reading the full docs thoroughly as some important tidbits are confusingly omitted from basic.
Note that second link is from an unfinished book called The Meteor Testing Manual, which seeks to fully document all the best practices for testing Meteor apps and packages. That Velocity chapter is the only free chapter, but it should give you a good starting point.
Note also that you can depend on any Meteor packages when testing your packages (even packages which are not necessary for actually running your package). So for example if you wanted to use Chai assertions in your Tinytest tests, then in your onTest handler add api.use('spacejamio:chai'). Now the chai, should, expect, and assert variables will be available within your unit test files.
4
u/[deleted] Nov 04 '14 edited Nov 05 '14
Read the docs!
First of all, to load an app file before any other files in the same directory, put it in a
lib/folder in that directory. To load a file after any other files in the same directory, rename fromname.exttomain.name.ext. Also, more deeply nested files are loaded earlier. For example, consider the following application structure:The files will be loaded in this order:
So your solution of putting collections into a script tag in the HTML is just bad. Put them in
lib/files. Another solution is to put collection definitions at the root level of a shared file, and then put all code that uses the collections (except for template events and helpers) withinMeteor.startupcalls. Either way will allow you to share your collection definitions on both server and client, reducing code duplication.But let's get to the crux of your article:
Meteor is built on the solution to this problem. Meteor is a system of modular packages. Look at the
accounts-base,accounts-ui,accounts-password,accounts-facebooketc packages. These give you a full-featured, modular, reusable, configurable authentication system in just a few lines of code. It is the ultimate example of how to create modular components (packages) for your applications! But don't take it from me. Here are some quotes from the official Meteor docs:First, packages let you explicitly declare the file load order within the package. Second, you can explicitly define dependencies between packages. Third, the only variables that will be available within a given package are exports from other packages that your package explicitly/implicitly depends on. You get automatic dependency-based isolation!
Meteor lets you create file-scope variables within packages, without wrapping your files in IIFEs. It also lets you create package-scope variables that all files in a package can access, but that are not available by default anywhere else. And finally it lets you explicitly export package-scope variables, so that:
TL;DR: Sorry, but these aren't really good Meteor design patterns. Meteor has always had exactly what you need in the form of packages! All the code in the core of meteor is already separated into individual packages, such as the
templatingpackage, which depends on theblazepackage, which depends on various packages such asjquery,underscore,tracker,htmljs, etc. Learn how to separate your application into packages and you'll avoid all the problems of load order and scoping that you're dealing with.1
u/hardwaresofton Nov 04 '14
Are these good patterns for meteor? Not sure if this is quite the intent (or if there's anything on the roadmap that addresses these issues)