<?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>Topper's Blog &#187; event</title>
	<atom:link href="http://blog.toppingdesign.com/tag/event/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.toppingdesign.com</link>
	<description>Usability, Information Management, Rails, Javascript &#38; (oh no) Web 2.0</description>
	<lastBuildDate>Tue, 24 Jan 2012 19:52:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>An Open-Source Event Handler</title>
		<link>http://blog.toppingdesign.com/2008/02/20/an-open-source-event-handler/</link>
		<comments>http://blog.toppingdesign.com/2008/02/20/an-open-source-event-handler/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 15:32:40 +0000</pubDate>
		<dc:creator>Topper</dc:creator>
				<category><![CDATA[General Web work - technical]]></category>
		<category><![CDATA[bubbling]]></category>
		<category><![CDATA[delegation]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[eventdelegation]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[prototype.js]]></category>

		<guid isPermaLink="false">http://blog.toppingdesign.com/2008/02/20/an-open-source-event-handler/</guid>
		<description><![CDATA[Event delegation and bubbling seems to have become a little bit of a hot-topic lately. We've been using a custom-written prototype.js based library for a while here at Motionbox and after reading some articles on Ajaxian and others, we've decided to open-source the library. As you know, the technique of listening to events high in [...]]]></description>
			<content:encoded><![CDATA[<p>Event delegation and bubbling seems to have become a little bit of a hot-topic lately. We've been using a custom-written prototype.js based library for a while here at Motionbox and after reading some articles on Ajaxian and others, we've decided to open-source the library.</p>
<p>As you know, the technique of listening to events high in the DOM adds a lot of benefits. The two most notable are:</p>
<p>1.  The ability to bind to events before the elements are available in the DOM. This is especially useful for AJAX applications where the DOM is being updated as there is no need to (re)create all your observers. Using this library, the developer doesn't care if an element exists or not, he/she can simply say "any thing with this class works this way" and "anything with this ID does this"  All with a simple interface:</p>
<div class="igBar"><span id="ljavascript-6"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=Iw==" onclick=\"javascript:showPlainTxt('javascript-6'); return false;\">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-6">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#myId"</span>, <span style="color: #3366CC;">"click"</span>, myHandlerFunction<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>2. Since we handle events outside of the usual manner, custom event handling becomes trivial.  The subscription interface is exactly the same, and custom events can be fired easily with:</p>
<div class="igBar"><span id="ljavascript-7"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=Iw==" onclick=\"javascript:showPlainTxt('javascript-7'); return false;\">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-7">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">fireCustom</span><span style="color: #66cc66;">&#40;</span>domElement, <span style="color: #3366CC;">"my_custom_event_name"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The docs for the library are here:  <a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NvZGUuZ29vZ2xlLmNvbS9wL21vdGlvbmJveC93aWtpL0V2ZW50SGFuZGxlckRvY3VtZW50YXRpb24=">http://code.google.com/p/motionbox/wiki/EventHandlerDocumentation</a></p>
<p>The files can be checked out here (SVN):  <a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vdGlvbmJveC5nb29nbGVjb2RlLmNvbS9zdm4vamF2YXNjcmlwdC9ldmVudF9oYW5kbGVyL3RydW5r">http://motionbox.googlecode.com/svn/javascript/event_handler/trunk</a><br />
or downloaded here:  <del datetime="2008-02-29T22:03:17+00:00"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vdGlvbmJveC5nb29nbGVjb2RlLmNvbS9maWxlcy9ldmVudF9oYW5kbGVyLTEuMC4wLmpz">http://motionbox.googlecode.com/files/event_handler-1.0.0.js</a></del><br />
<ins datetime="2008-02-29T22:03:17+00:00"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vdGlvbmJveC5nb29nbGVjb2RlLmNvbS9maWxlcy9ldmVudF9oYW5kbGVyLTEuMC4xLmpz">http://motionbox.googlecode.com/files/event_handler-1.0.1.js</a></ins></p>
<p>You can see it in action throughout Motionbox.com (used a lot on our contests and Motionbooks pages:  <a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vdGlvbmJveC5jb20vY29udGVzdHMvdmFsZW50aW5lcw==">http://motionbox.com/contests/valentines</a> and <a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21vdGlvbmJveC5jb20vbW90aW9uYm9va3MvbmV3">http://motionbox.com/motionbooks/new</a> --login required).</p>
<p>Quoting a code example from the docs:</p>
<div class="igBar"><span id="lhtml-8"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=Iw==" onclick=\"javascript:showPlainTxt('html-8'); return false;\">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-8">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function my_listener(evt) {</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;console.dir(evt);</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">}</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlY2VtYmVyLmNvbS9odG1sLzQvZWxlbWVudC9ib2R5Lmh0bWw="><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlY2VtYmVyLmNvbS9odG1sLzQvZWxlbWVudC91bC5odG1s"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"unordered_list"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"my_ul_class"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlY2VtYmVyLmNvbS9odG1sLzQvZWxlbWVudC9saS5odG1s"><span style="color: #000000; font-weight: bold;">&lt;li</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"one"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"my_li_class"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>First<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2RlY2VtYmVyLmNvbS9odG1sLzQvZWxlbWVudC9saS5odG1s"><span style="color: #000000; font-weight: bold;">&lt;li</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"two"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"my_li_class"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>Second<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>You can now:</p>
<div class="igBar"><span id="ljavascript-9"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=Iw==" onclick=\"javascript:showPlainTxt('javascript-9'); return false;\">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-9">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#one"</span>, <span style="color: #3366CC;">"click"</span>, my_listener<span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">// this will trigger 'my_listener' only by clicking on the li with the id of &quot;one&quot;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// or</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">".my_li_class"</span>, <span style="color: #3366CC;">"click"</span>, my_listener<span style="color: #66cc66;">&#41;</span>;&nbsp; <span style="color: #009900; font-style: italic;">// this will trigger 'my_listener' by clicking either of the LIs below</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// All three of the required arguments may be an array:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">".my_ul_class"</span>, <span style="color: #3366CC;">"#one"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">"click"</span>, <span style="color: #3366CC;">"mouseover"</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #66cc66;">&#91;</span>my_listener, my_second_listener<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>NOTE: Remember that you can subscribe to these IDs or classes at ANY time after the MBX.EventHandler object is available (well before the dom is ready).</p>
<p>Since we are handling these events outside of the normal browser implementation, you are not confined to browser events:</p>
<div class="igBar"><span id="ljavascript-10"><a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=Iw==" onclick=\"javascript:showPlainTxt('javascript-10'); return false;\">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JavaScript:</span>
<div id="javascript-10">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#one"</span>, <span style="color: #3366CC;">"my_custom_event_name"</span>, my_listener<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// which you could then fire with:</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">fireCustom</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"one"</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #3366CC;">"my_custom_event_name"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// fireCustom also allows the optional extension of custom events with any data of your choosing:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MBX.<span style="color: #006600;">EventHandler</span>.<span style="color: #006600;">fireCustom</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"one"</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #3366CC;">"my_custom_event_name"</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'customAttribute'</span>: <span style="color: #3366CC;">"this is cool"</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>See the complete docs here: <a href="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2NvZGUuZ29vZ2xlLmNvbS9wL21vdGlvbmJveC93aWtpL0V2ZW50SGFuZGxlckRvY3VtZW50YXRpb24=">http://code.google.com/p/motionbox/wiki/EventHandlerDocumentation</a></p>
 <img src="http://blog.toppingdesign.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=10" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.toppingdesign.com/2008/02/20/an-open-source-event-handler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

