Hydra.js
Hydra.js is a module manager oriented system.
Updated to version 3.11.0 + ES6
Description
Hydra.js is the library that will help you to scale your app. Hydra.js is a framework that gives you the tools to write your application using modules or widgets and make easy to work with them.
Hydra.js uses a decoupled architecture that:
- Allows you to change your base framework without change the modules or widget code.
- Allow the modules communicate with each other without knowing which modules are loaded.
- Can be easily extended with new features.
Benefits:
- No known module to other modules
- If something is wrong in one module, the other modules will continue working.
- Notifying an action will be called on all the modules that will be listening this action.
- A module can be extended
- If you have a module that is working well you can extend it to change his behavior without losing is original behavior.
- Allows multi-instance modules
- Allows set private variables to be used inside of modules.
- Can be used in url threaded application as in an Ajax threaded application.
- You can test your modules with any Unit Testing Framework.
- Only KB when Gzipped.
Usage
Install:
Install with Bower
bower install hydrajs
Install with Component
component install hydrajs
Install with NPM
npm install hydra.js
Use in browser
Insert in your html code:
Common usage
Setting variables
Hydramodule;
Setting the variables in this way will make the variables accessible as the last argument in the init module method. If needed you can also access these variables using getVars (See 'Getting variables')
*Tip. This method not only sets variables, if the object has been set before the new variables will be merged with the previous object. *
Getting variables
var oVars = Hydramodule;
Returns the object with the private variables set using setVars (See 'Setting variables')
Module creator function
The module creator function gets four arguments:
- Bus
- Get access to Hydra.bus, the action manager to publish or subscribe to events
- Module
- Get access to Hydra.module, the module manager to register, extend, decorate, start and stop modules.
- ErrorHandler
- Get access to the Hydra.errorHandler, it's recommended to use it instead of using console.log because of the possible improvements see Hydra.js extensions or Hermes.js
- Api
- Get access to the rest of the Hydra api. You can use it to access to the current extensions, i.e. jQuery, or to your own extensions.
{ return {} ;}
Create a module
Hydramodule;
Create a module using dependency injection
Hydramodule;
The following are available for dependency injection:
- $bus
- $module
- $log (error handler)
- $api
- $global (window object)
- $doc (document object)
To use dependency injection, pass an array of strings containing any of the variables listed above as the second parameter when registering a module. Hydra will pass them in the order specified to your function.
Extend a module overriding the base module
To extend a module you will need to register the base module before extends it.
Hydramodule;
Extend a module creating a new module
To extend a module you will need to register the base module before extends it.
Hydramodule;
This extension allows access the parent methods as classical inheritance.
Access parent methods
Register base module:
Hydramodule;
Create the new module using "extend":
Hydramodule;
Decorating modules
Sometimes is better to decorate our modules instead of extending them. I recommend to use decorate instead of extend modules.
Hydramodule;
Listening to events
Hydramodule;
Publishing actions
To use the action manager you have accessible using "Bus".
The publish method expect three arguments, but only the first two are mandatory, the channel name and the event name
Hydrabus;
*Tip: 'global' channel is created by default to use it if you want to communicate with other modules that are not related with a specific channel. *
Hydramodule;
Preprocessing Data on publish actions.
When the data is pass to the listener actions it gives the same object by default, if you need to pre-process this data before execute the action, you should use the 'preprocessorPublishData' method in Hydra.bus. The callback that you pass will get two arguments:
- The data object
- The clone method used in Hydra.js.
Example of a code that returns a copy of the data object
Hydrabus;
If you need compatibility with the previous event manager called Action, you can add it in your code to maintain compatibility with previous version's code. You can download it from: Action
Documentation
License
Hydra.js is licensed under the MIT license.
Agreements
Hydra was inspired by Nicholas Zakas presentation.