hapi-mol
Simple module loader plugin for hapi.js
Mol allows you to easily manage your app across multiple files. For a full guide see the examples.
Usage
var hapi = mol = server = debug: request: 'error' ; server;server;
Register mol as any other Hapi plugin. You can pass the following configuration when registering the plugin -
base
the base path from which each folder will be loaded default(./
)tree
if true the scope passed to theregisterCb
will be a node of the original scope relative to the internal path default(true)
Mol exposes a load
method on server.plugins.mol
.
the load
method recursively loads a folder under the base
path
You can pass the folowing parameters to the load
method -
loadPath
- path to load files fromscope
(optional) - the scope object which will be passed to the register callback ifflat
isfalse
scope will be relative to subfolders so when calling the register callback for 'base_path/foo/bar.js' scope will be scope['foo']registerCb
- the callback that will be called for each file inloadPath
- `cb (optional) - callback to call once all files are loaded, since Mol uses promises you can leave it blank and just follow the promise chain
Since the load
method return a Promise you don't have to pass a callback and you can just use the Promise chain.
registerCb
is where the real magic should happen.
Mol will call registerCb
for each file under loadPath
and will pass the following parameters -
path' The load path of the current file under the
localPath`- `base' the base name of the file (no path no extension)
- `ext' the extension of the file
- `localScope' the scope node the file belongs to (if tree is false the root scope)
parts
Array of folders under loadPath where the file is located
If you prefer you can use Mol as a regular library instead of a hapi plugin. to do so Mol exposes a construction function you can use it as follows -
var mol = load = ;;