minioc-loader

Utility for loading and initializing minioc aware modules (nodejs).

minioc-loader

Utility for loading and initializing minioc aware modules (nodejs).

minioc is a miniature IoC container for nodejs - see the github repo for more info.

var loader = require('minioc-loader')({ basePath: __dirname })
, minioc = loader.minioc // minioc is exported for convenience 
;
 
loader.loadSync(minioc.root, './config.js');
loader.loadSync(minioc.root, './lib/');
 
// standard minioc here, our startup function has two 
// dependencies that will be injected by minioc... 
minioc.fulfill('startup', function($config$app) {
 
    // assuming that the two paths loaded above result in registrations 
    // being made for both `$config` and `$app` objects, minioc will call 
    // this function. 
 
    //... do your stuff ... 
    console.log('nothing to see here');
 
});
  • basePath - required base directory path, used to resolve relative paths during loadSync calls
  • loadSync - loads a module or a directory according to the minioc-loader convention
  • container (argument 0) - a minioc container used when initializing modules
  • path (argument 1) - a path (relative to loader's basePath) to be loaded. Can be a .js module or a directory.

When calling loadSync...

If path is a .js file then the loader will treat the file as a module and load it using node's require function. If the module exports a function with the name $init then the loader will tell minioc to fulfill it. Minioc identifies dependencies by convention; any named argument beginning with a dollar sign $ is considered a dependency and minioc will inject that argument. As soon as minioc is able to fulfill all of the dependencies of the exported $init function minioc does so. When minioc calls the exported $init function, this is bound to the IoC container previously given to loadSync.

If path is a directory, the loader will look for an index.js file. If one is present, only the index.js file is loaded as a module; otherwise, all .js files in the directory are loaded.

Study the example code to understand it fully, in particular, notice that the order in which registrations occur on the container is unimportant because minioc will fulfill all requests as soon as their dependencies can be met.

You may also find these other projects useful...

  • minioc-broadway - Broadway plugin for adding minioc and minioc-loader to broadway/flatiron apps.