<?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>Garrick Cheung &#187; event delegation</title>
	<atom:link href="http://www.garrickcheung.com/tag/event-delegation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.garrickcheung.com</link>
	<description>Sharing what I know and learn about CSS, MooTools, Javascript, PHP and etc.</description>
	<lastBuildDate>Fri, 30 Sep 2011 05:16:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Aaron Newton&#8217;s Event Delegation for MooTools</title>
		<link>http://www.garrickcheung.com/mootools/aaron-newtons-event-delegation-for-mootools/</link>
		<comments>http://www.garrickcheung.com/mootools/aaron-newtons-event-delegation-for-mootools/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 19:00:57 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[MooTools]]></category>
		<category><![CDATA[event delegation]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=252</guid>
		<description><![CDATA[I just learned of Aaron Newton&#8217;s Event.Delegate.js and think it is amazing. Why is it amazing? Event delegation is a common practice where by you attach an event listener to a parent object to monitor its children rather than attach events to all the children. It’s far more efficient when you have numerous items on [...]]]></description>
			<content:encoded><![CDATA[<p>I just learned of <a href="http://www.clientcide.com/code-releases/event-delegation-for-mootools/" target="_blank">Aaron Newton&#8217;s Event.Delegate.js</a> and think it is amazing. Why is it amazing?</p>
<blockquote><p>
Event delegation is a common practice where by you attach an event listener to a parent object to monitor its children rather than attach events to all the children. It’s far more efficient when you have numerous items on a page that you want to interact with.</p></blockquote>
<p>The examples below should give you a better idea how awesome it is.<br />
<span id="more-252"></span></p>
<h3>The Problem</h3>
<p>Say you have A LOT of anchors and you want to add a click event to them. Normally you&#8217;d do this in several ways:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// First method</span>
<span style="color: #003366; font-weight: bold;">var</span> links <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
links.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>link<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    link.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//Do this stuff</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Second method</span>
$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//Do this stuff</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is all fine but it&#8217;s not very efficient. You end up looping through all the anchors to add the event.</p>
<h3>The Solution</h3>
<p>With Event.Delegate you can attach an event to the parent, and have it handle everything. I&#8217;m going to use the example from Aaron&#8217;s post with a small addition to what I have above.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//Parent element handles the delegation, and here it's document.body</span>
document.<span style="color: #660066;">body</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'a.link'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//Just like addEvent, you can pass in the event and stop it.</span>
    e.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'you clicked a link!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>I really like Event Delegation. I&#8217;m really curious how fast it is though. Time to do a couple console.profile() to log the times.</p>
<p>I didn&#8217;t understand this part of his post:</p>
<blockquote><p>Finally, you can pass in a function of your own. If your function returns true, then your method will fire. Note that the element passed to your method is not extended by MooTools yet for efficiency. You should avoid extending it yourself (by passing it through $) as this will affect performance. Instead, use the static methods on Element.
</p></blockquote>
<p>It confused me. I thought he was talking about the third argument, until he mentioned it&#8217;s a function that returns true. Passing in a function as a test looks something like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//My test function</span>
<span style="color: #003366; font-weight: bold;">var</span> testFunc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> target.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.link'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
document.<span style="color: #660066;">body</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> testFunc<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    e.<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'you clicked a link!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Thanks for writing about this Aaron. It&#8217;s a really great addition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/mootools/aaron-newtons-event-delegation-for-mootools/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

