<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Perplexed Labs &#187; event handler</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/event-handler/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Thu, 09 Sep 2010 17:11:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JavaScript Event Handler</title>
		<link>http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/</link>
		<comments>http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 15:34:17 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[event handler]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/?p=65</guid>
		<description><![CDATA[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 [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>2008-11-16: Updated the source code to remove the Prototype dependency</strong></p>
<p>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().</p>
<pre class="brush: jscript;">
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 &lt; len; ++i) {
			action = this.actions[idx][i];
			action(args);
		}
	}
};

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


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2008/12/22/jquery-13-beta-testing/' rel='bookmark' title='Permanent Link: jQuery 1.3 Beta Testing'>jQuery 1.3 Beta Testing</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/11/07/real-time-ajax-javascript-progress-bar/' rel='bookmark' title='Permanent Link: Real-time &#8220;AJAX&#8221; JavaScript Progress Bar'>Real-time &#8220;AJAX&#8221; JavaScript Progress Bar</a></li>
<li><a href='http://blog.perplexedlabs.com/2009/02/05/dynamic-javascript-script-insertion-for-embedding/' rel='bookmark' title='Permanent Link: PHP Dynamic JavaScript SCRIPT Insertion for Embedding'>PHP Dynamic JavaScript SCRIPT Insertion for Embedding</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2008/11/12/javascript-event-handler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
