<?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>DaveOnCode &#187; event listener</title>
	<atom:link href="http://www.daveoncode.com/tag/event-listener/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +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>Centralizing event handling in Actionscript 3: a good method to handle events for multiple objects with a single listener</title>
		<link>http://www.daveoncode.com/2008/12/22/centralizing-event-handling-in-actionscript-3-a-good-method-to-handle-events-for-multiple-objects-with-a-single-listener/</link>
		<comments>http://www.daveoncode.com/2008/12/22/centralizing-event-handling-in-actionscript-3-a-good-method-to-handle-events-for-multiple-objects-with-a-single-listener/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 14:26:21 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[event handling]]></category>
		<category><![CDATA[event listener]]></category>
		<category><![CDATA[listener]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=157</guid>
		<description><![CDATA[One of the first cool things I learned about Actionscript 3, is how to handle events. Actionscript 3 is heavily tied with events, so the use and a deep understanding of events and listener should be one of the most important subject to face for an Actionscript developer. Although I&#8217;m far away to be considered [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/22/centralizing-event-handling-in-actionscript-3-a-good-method-to-handle-events-for-multiple-objects-with-a-single-listener/' addthis:title='Centralizing event handling in Actionscript 3: a good method to handle events for multiple objects with a single listener ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>One of the first cool things I learned about Actionscript 3, is how to handle events. Actionscript 3 is heavily tied with events, so the use and a deep understanding of events and listener should be one of the most important subject to face for an Actionscript developer. Although I&#8217;m far away to be considered an experienced Actionscript developer, I&#8217;ve just realized the power and the benefits of a centralized event handling architecture and I want to share my new knowledge by writing this post.  So&#8230; what means to &#8220;centralize events handling&#8221;?<br />
<span id="more-157"></span><br />
Let&#8217;s start with a basic example of event handling, in which we attach a listener to an object, which simply will listen for a <strong><em>MouseEvent.MOUSE_OVER</em></strong> event:</p>
<pre>
myObject.addEventListener(MouseEvent.MOUSE_OVER, myObject.mouseOverHandler);
</pre>
<p>Now, let&#8217;s suppose that we need to handle even a <strong><em>MouseEvent.MOUSE_OUT</em></strong> event, we can simply add a new listener in this way:</p>
<pre>
myObject.addEventListener(MouseEvent.MOUSE_OUT, myObject.mouseOutHandler);
</pre>
<p>So the resulting code should be something like:</p>
<pre>
package {

    import flash.display.Sprite;

    public class MyObject extends Sprite {

        public function MyObject() {

            this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOutHandler);

        }

        private function mouseOverHandler(e:MouseEvent):void {

            // do something in response to MouseEvent.MOUSE_OVER event

        }

        private function mouseOutHandler(e:MouseEvent):void {

            // do something in response to MouseEvent.MOUSE_OUT event

        }

    }

}
</pre>
<p>Pretty straight and pretty clear, but let&#8217;s suppose that we have to deal with several &#8220;MyObject&#8221;, this approach will means to create an huge number of listeners which will increase memory usage and decrease application&#8217;s performance. One clever way to solve the problem and optimize code and performance is to delegate the event handling to a single listener, which handlers will use the Event&#8217;s <strong><em>target</em></strong> property in order to apply the code to the desired object.<br />
A concrete example could be a scenario in which we have an application&#8217;s menu (represented by an <strong><em>AppMenu</em></strong> class) which contains several buttons (represented by an <strong><em>AppMenuItem</em></strong> class). The only listener will be related to <em><strong>AppMenu</strong></em> and will handle the MOUSE_OVER/MOUSE_OUT for each <strong><em>AppMenuItem</em></strong>:</p>
<pre>
package {

    import flash.display.Sprite;

    public class AppMenu extends Sprite {

        public function AppMenu() {

            this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOverHandler);

        }

        private function mouseOverHandler(e:MouseEvent):void {

            var ancestor:DisplayObject = DisplayObject(e.target).parent;

            if (e.target is AppMenuItem|| ancestor is AppMenuItem) {
                // activates the AppMenuItem
                AppMenuItem(ancestor is AppMenuItem ? ancestor : e.target).setActiveState();
                // subscribes for mouseOut event
                this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            }

        }

        private function mouseOutHandler(e:MouseEvent):void {

            var ancestor:DisplayObject = DisplayObject(e.target).parent;

            if (e.target is AppMenuItem || ancestor is AppMenuItem) {
                // deactivates the AppMenuItem
                AppMenuItem(ancestor is AppMenuItem ? ancestor : e.target).setDefaultState();
                // unsubscribes mouseOut listener
                this.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            }

        }

    }

}
</pre>
<p>In the example above we simply attach only one listener for the <strong><em>MouseEvent.MOUSE_OVER</em></strong>, attaching a listener for <strong><em>MouseEvent.MOUSE_OUT</em></strong> is initially unnecessary because the event will occurs only after the previous event (and potentially it can never be used) , so the &#8220;cheapest&#8221; approach that we can have is to add the second listener only when necessary, that is when MOUSE_OVER occurs and remove it as soon as is used, that is inside <em><strong>mouseOverHandler()</strong></em> code. Both handlers (mouseOverHandler and mouseOutHandler) will invoke setActiveState() or setDefaultState() methods, after verify that the target of the event is an <strong><em>AppMenuItem</em></strong> or a child of it.<br />
In this scenario, if we have 1 or 1000 AppMenuItem objects, we can handle 2 events for each object with only one listener, with the first approach we will have 2*N listeners, which means that to handle 1000 AppMenuItem we will have 2000 listeners!<br />
As we can see, centralizing events handling is a really, really good thing!</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2008/12/22/centralizing-event-handling-in-actionscript-3-a-good-method-to-handle-events-for-multiple-objects-with-a-single-listener/' addthis:title='Centralizing event handling in Actionscript 3: a good method to handle events for multiple objects with a single listener ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2008/12/22/centralizing-event-handling-in-actionscript-3-a-good-method-to-handle-events-for-multiple-objects-with-a-single-listener/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

