Combining Modules in Require.js


Here’s a quick tip that I learned today the hard way, because it’s actually in the documentation.

In one of my projects, I’ve got a bunch of commands that I want to attach to an event based on what menu item is selected.  My app object listens for menu events and then wires up the command based on the selected menu item.  My first version looked like this:

This is module definition is obviously going to get very messy as I add more available commands.  The solution was to create a new module to combine all of the commands and use a different variation on the module definition, where you’re only dependency is require itself.

Now I can simplify my app module to look like this

Node.js Must Know Concepts: Asynchronous