DiModular
Small and versatile framework for developing scalable JavaScript applications. DiModular is inspired by the Scalable JavaScript Application Architecture presented by by Nicholas Zakas, also known as the Core-Module-Sandbox pattern. However, instead of a "sandbox", a dependency injection container is used for handling dependencies and extending core functionality.
Installation
$ npm install di-modular
Api
diModular.register(dependency, value)
Registers value as named dependency.
diModular;
diModular.factory(dependency, factory)
Registers factory as named dependency. Factories can lists registered dependencies in their argument list, which will be injected when the factory is called.
diModular;
Alternative syntax that is more resilient to code minifcation and name mangling:
diModular;
diModular.get(dependency)
Access named dependency.
diModular.module(name, moduleFactory)
Registers a named module factory. Module factories can lists registered
dependencies in their argument list, which will be injected when the module is
instantiated. Modules should implement an init
method and a destroy
method,
which will be called as the module is started and stopped respectively.
diModular;
Alternative syntax that is more resilient to code minification and name mangling:
diModular;
diModular.start(name[, arg1][, arg2][, ...])
Starts named module. The module's init
method is called. If provided, optional
arguments are passed to the init
method.
diModular.startAll([, arg1][, arg2][, ...])
Starts all non-running modules. Each started module's init
method is called.
If provided, optional arguments are passed to each init
method.
diModular.stop(name)
Stops named module. The module's destroy
method is called.
diModular.stopAll()
Stops all running modules. Each stoped module's destroy
method is called.
Example
var EventEmitter = EventEmitter;var DiModular = ; var modular = ; // Register named value dependencymodular; // Register named factory dependencymodular; modular; // Access the event emitter and listen for `create` event.modular; // Access the event emitter and listen for `destroy` event.modular; // Start module and pass in some optionsmodularstart'honda' sound: 'Honk, honk'; // Access the event emitter and emit `honk` event.modular; // Stop modulemodular; // Output:// > honda has been created// > honda says: "Honk honk!"// > honda has been destroy
Test
Run unit tests;
$ npm test
Create test coverage report:
$ npm run-script test-cov