2008-11-16: Updated the source code to remove the Prototype dependency

This is a very simple, lightweight, object to manage the handling of events in JavaScript. Events can be arbitrarily defined and identified based on calls to listen(). Multiple actions can be handled per event and events can be triggered arbitrarily at any point in your code with calls to trigger().

var EventHandler = {
	events: [],
	actions: [],
	index: {}
};

EventHandler.listen = function(evt, action)
{
	var idx = this.events.length;

	// add a new event entry if one doesn't exist already
	if(typeof(this.index[evt]) == 'undefined') {
		this.events[idx] = evt;
		this.index[evt] = idx;
		this.actions[idx] = [];
	}

	// add to the list of actions for this event
	this.actions[idx][this.actions[idx].length] = action;
};

EventHandler.trigger = function(evt, args)
{
	var idx = this.index[evt];

	if(typeof(idx) != 'undefined') {
		// cycle and call the actions for this event
		for(var i = 0, len = this.actions[idx].length; i < len; ++i) {
			action = this.actions[idx][i];
			action(args);
		}
	}
};

/*
EventHandler.listen('test', function() { alert('Testing Event Handler'); });
EventHandler.trigger('test');
*/

Related posts:

  1. jQuery 1.3 Beta Testing
  2. Real-time “AJAX” JavaScript Progress Bar
  3. Converting from Prototype to jQuery
  4. PHP Dynamic JavaScript SCRIPT Insertion for Embedding
  5. PHP jQuery AJAX Javascript Long Polling