Icontem

Hydra.js: Modular event action handler

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (27)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2011-07-21 (4 years ago) RSS 2.0 feedNot enough user ratingsTotal: 212 All time: 197 This week: 84Up
Version License JavaScript version Categories  
hydra 1.0MIT/X Consortium ...1.0Language
Description Author  

This package implements a modular event action handler.

It can register event handler module objects that can execute actions when an event is triggered.

Each module can be extended by a separate handler module objects.

Picture of Tomas Corral
Name: Tomas Corral <contact>
Classes: 2 packages by
Country: Spain Spain
Age: ???
All time rank: 684 in Spain Spain
Week rank: 56 Up2 in Spain Spain Up

Details provided by the author  
# Hydra.js
Hidra.js is a module manager oriented system.

## Description

Hydra is used to create a scalable, maintainable and module oriented system.
Hydra is framework agnosthic.
Hydra has been designed to create your application in a modular design system.

### Some benefits:

* No known module to other modules
 * If something is wrong in one module, the others 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.
* Can be used in url threaded application as in a ajax threaded application.
* You can test your modules with any Unit Testing Framework.
* Only 1.1kb when Gzipped.

[API documentation](https://github.com/tcorral/Hydra.js/examples_and_documents/jsdoc/index.html)

[Examples](https://github.com/tcorral/Hydra.js/examples_and_documents/index.html) to see for yourself!

## Usage

### Before using it:
Insert in your code:

	<script type="text/javascript" src="/path/to/your/js/libs/Hydra.js"></script>
	
### Create a module
	Hydra.module.register('moduleId', function(action)
	{
		return {
			init: function (oData) {},
			handleAction: function (oNotifier){
			},
			destroy: function () {}
		};
	});

### Extend a module
To extend a module you will need to register the base module before extend it.

	Hydra.module.extend('moduleId', function(action)
	{
		return {
			init: function (oData) {},
			handleAction: function (oNotifier){
			},
			destroy: function () {}
		};
	});

### Listening actions
To use the action manager you have accessible using "action".

*Tip: Use it on your init to start listening actions when the module starts.*

#### One action
	Hydra.module.register('moduleId', function(action)
	{
		return {
			init: function (oData) {
				action.listen(['action1'], this.handleAction, this);
			},
			handleAction: function (oNotifier){
			},
			destroy: function () {}
		};
	});

#### More actions
	Hydra.module.register('moduleId', function(action)
	{
		return {
			init: function (oData) {
				action.listen(['action1', 'action2'], this.handleAction, this);
			},
			handleAction: function (oNotifier){
				switch(oNofitier.type)
				{
					case 'action1':
						/* your code */
						break;
					case 'action2':
						/* your code */
						break;
					default: break;
				}
			},
			destroy: function () {}
		};
	});
	
*Tip: If you have several actions to listen I recommend to make use of an object where the keys must be the names of the actions.*

	Hydra.module.register('moduleId', function(action)
	{
		return {
			init: function (oData) {
				action.listen(['action1', 'action2', 'actionN'], this.handleAction, this);
			},
			actionHandlers: {
				action1: function (oData) {},
				action2: function (oData) {},
				actionN: function (oData) {}
			},
			handleAction: function (oNotifier){
				var oHandler = this.actionHandlers[oNotifier.type]
				if(typeof oHandler === "undefined")
				{
					return;
				}
				oHandler.call(this, oData);
				oHandler = null;
			},
			destroy: function () {}
		};
	});

### Notifying actions
To use the action manager you have accessible using "action".

The notify method needs a Notifier object:

	var oNotifier = {
		type: 'listenedAction',
		data: 'data'
	};

*Tip: You can notify an action where ever you want inside the module using 'action'*

	Hydra.module.register('moduleId', function(action)
	{
		return {
			init: function (oData) {
				action.listen(['action1', 'action2', 'actionN'], this.handleAction, this);
				$("#button").click(function(){
					action.notify({
						type: 'listenedAction',
						data: 'data'
					});
				});
			},
			actionHandlers: {
				action1: function (oData) {},
				action2: function (oData) {},
				actionN: function (oData) {}
			},
			handleAction: function (oNotifier){
				var oHandler = this.actionHandlers[oNotifier.type]
				if(typeof oHandler === "undefined")
				{
					return;
				}
				oHandler.call(this, oData);
				oHandler = null;
			},
			destroy: function () {}
		};
	});
*Tip: You can create an Action on your code to notify some module creating a new instance of it in this way:*

	var oAction = new (Hydra.action());
		oAction.notify({
			type: 'listenedAction',
			data: 'data'
		});

## Documentation

[API documentation](https://github.com/tcorral/Hydra.js/examples_and_documents/jsdoc/index.html)

[Examples](https://github.com/tcorral/Hydra.js/examples_and_documents/index.html) to see for yourself!

## License

Hydra.js is licensed under the MIT license.

## Agreements

Hydra was inspired by Nicholas Zakas presentation.

* [Scalable Javascript Application](http://www.slideshare.net/nzakas/scalable-javascript-application-architecture)
  Files folder image Files  
File Role Description
Files folder imageexamples_and_documents (5 files, 2 directories)
Files folder imagelibs (2 files)
Files folder imagesrc (1 file)
Files folder imagetest (1 file)
Accessible without login Plain text file jsTestDriver.conf Data Config for jstestdriver
Accessible without login Plain text file LICENSE.txt Lic. License
Accessible without login Plain text file Readme.md Doc. Readme markdown

  Files folder image Files  /  examples_and_documents  
File Role Description
Files folder imagejs (7 files)
Files folder imagejsdoc (2 files, 1 directory)
  Accessible without login HTML file index.html Data Index page of samples
  Accessible without login Plain text file singleModule.html Example Example usage single module
  Accessible without login Plain text file singleModuleWithListener.html Example Example usage single module add listeners
  Accessible without login Plain text file twoModules.html Example Example usage two modules
  Accessible without login Plain text file twoModulesOneListenOtherNotify.html Example Example usage two modules one listen other notify

  Files folder image Files  /  examples_and_documents  /  js  
File Role Description
  Accessible without login Plain text file sampleExtendModule.js Example Example extend module
  Accessible without login Plain text file sampleModule.js Example Example simple module
  Accessible without login Plain text file sampleModule2.js Example Example simple module 2
  Accessible without login Plain text file sampleModuleError.js Example Example simple module with error
  Accessible without login Plain text file sampleModuleListener.js Example Example module implement listener
  Accessible without login Plain text file sampleModuleNotifier.js Example Example module notifier
  Accessible without login Plain text file sampleModuleWithListener.js Example Example module with listener

  Files folder image Files  /  examples_and_documents  /  jsdoc  
File Role Description
Files folder imagesymbols (5 files, 1 directory)
  Accessible without login HTML file files.html Doc. JSDoc
  Accessible without login HTML file index.html Doc. JSDoc

  Files folder image Files  /  examples_and_documents  /  jsdoc  /  symbols  
File Role Description
Files folder imagesrc (1 file)
  Accessible without login HTML file Action.html Doc. JSDoc
  Accessible without login HTML file ErrorHandler.html Doc. JSDoc
  Accessible without login HTML file Hydra.html Doc. JSDoc
  Accessible without login HTML file Module.html Doc. JSDoc
  Accessible without login HTML file _global_.html Doc. JSDoc

  Files folder image Files  /  examples_and_documents  /  jsdoc  /  symbols  /  src  
File Role Description
  Accessible without login HTML file app_Hydra_Hydra.js.html Data JSDoc

  Files folder image Files  /  libs  
File Role Description
  Plain text file sinon.js Class Unit testing library
  Accessible without login Plain text file sinon_ie.js Aux. Unit testing library

  Files folder image Files  /  src  
File Role Description
  Plain text file Hydra.js Class Class Hydra code

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file HydraTest.js Test Unit testing code

 Version Control Unique User Downloads Download Rankings  
 0%Total:212All time:197
 This week:0This week:84Up