r/Meteor • u/hardwaresofton • Nov 04 '14
Good Meteor Patterns?
http://vadosware.com/2014/11/02/meteor-patterns/
0
Upvotes
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)
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:
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
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.