<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html" />
  <link rel="self" type="application/atom+xml" href="http://insideria.com/atom.xml" />
  <id>tag:insideria.com,2010://34/tag:www.insideria.com,2009://34.35664-</id>
  <updated>2010-07-16T15:52:28Z</updated>
  <title>Comments for Disabling events in Flex (http://insideria.com/2009/12/disabling-events.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.35664</id>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=35664" title="Disabling events in Flex" />
    <published>2009-12-16T17:00:00Z</published>
    <updated>2009-12-16T17:00:00Z</updated>
    <title>Disabling events in Flex</title>
    <summary><![CDATA[Flex does not provide a way to temporarily disable event listeners and re-enable them later.&nbsp; You may want to do this to prevent endless loops.&nbsp; For example, when some code modifies the selectedIndex of a List, an event is fired.&nbsp;...]]></summary>
    <author>
      <name>Mike Slinn</name>
      <uri>http://mslinn.com</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://insideria.com/">
      <![CDATA[<p>Flex does not provide a way to temporarily disable event listeners and re-enable them later.&nbsp; You may want to do this to prevent endless loops.&nbsp; For example, when some code modifies the <tt>selectedIndex</tt> of a List, an event is fired.&nbsp; If you have a listener that reacts to the event, you may need a way of suppressing the listener's response under certain conditions.&nbsp; You have two choices:</p><ul><li>Couple the listener to the code that generates the event, so it knows when to ignore the event.&nbsp; This defeats one of the purposes of event-driven programming, separation of concerns.</li><li>Suppress the event dispatch</li></ul>My gift to you today, dear reader, is the <tt>EventManager</tt> class.&nbsp; This class remembers all event listeners assigned to an <tt>EventDispatcher</tt>, and can remove or re-instate those listeners with a single method call.<br /><br />
<pre>package events {
    import flash.events.EventDispatcher;
    
    public class EventManager {
        private var dispatcher:EventDispatcher;
        private var type:String;
        private var listener:Function;
        private var useCapture:Boolean;
        private var priority:int;
        private var useWeakReference:Boolean;
        
        public function EventManager(dispatcher:EventDispatcher, type:String, listener:Function, useCapture:Boolean, priority:int, useWeakReference:Boolean) {
            this.dispatcher = dispatcher;
            this.type = type;
            this.listener = listener;
            this.useCapture = useCapture;
            this.priority = priority;
            this.useWeakReference = useWeakReference;
        }
        
        private static function disableFn(item:*, index:int, array:Array):void {
            var em:EventManager = EventManager(item);
            em.dispatcher.removeEventListener(em.type, em.listener, em.useCapture);
        }
        
        public static function disableAll(listeners:Array):void {
            listeners.forEach(disableFn);
        }
        
        private static function enableFn(item:*, index:int, array:Array):void {
            var em:EventManager = EventManager(item);
            em.dispatcher.addEventListener(em.type, em.listener, em.useCapture, em.priority, em.useWeakReference);
        }
        
        public static function enableAll(listeners:Array):void {
            listeners.forEach(enableFn);
        }
    }
}</pre>
Here is how you could use EventManager:<br /><br /><pre>package {<br />    public class EMDemo extends ComboBox {<br />        private var listeners:Array = new Array();<br />    	<br />    	override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {<br />    	    super.addEventListener(type, listener, useCapture, priority, useWeakReference);<br />    	    listeners.push(new EventManager(this, type, listener, useCapture, priority, useWeakReference));<br />    	}<br />        		<br />        /** Prevent update events from being issued */<br />        public function quietlySetIndex(newIndex:int):void {<br />            EventManager.disableAll(listeners);<br />            selectedIndex = newIndex;<br />            EventManager.enableAll(listeners);<br />        }<br />    }<br />}</pre><pre><span class="Apple-style-span" style="font-family: arial, helvetica, hirakakupro-w3, osaka, 'ms pgothic', sans-serif; white-space: normal; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.75em; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 1em; font-weight: normal; ">_______________________________</p><div>Mike Slinn<br />Independent Flex / Java contractor and author<br /><a href="http://slinnbooks.com/" style="text-decoration: underline; ">http://slinnbooks.com</a><br /><a href="http://www.mslinn.com/" style="text-decoration: underline; ">http://mslinn.com</a></div></span>
<img src="http://mslinn.com/sites/flex/images/ACE_FlexAIR.png" alt="Adobe Flex Certified Expert" /></pre>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2229687</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2229687" />
    <title>Comment from Amy on 2009-12-16</title>
    <author>
        <name>Amy</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>That's very cool.  You could probably use a similar technique to let you use event pooling to improve performance.</p>

<p>For other readers who may not have run across the concept of event pooling, it turns out that you don't have to create a new event each time you want to dispatch one...instead you can redispatch one you created earlier.  This means that you can keep a pool of events and remove the overhead of instantiating the event objects.</p>]]>
    </content>
    <published>2009-12-16T18:07:03Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2230610</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2230610" />
    <title>Comment from huiles de poisson on 2009-12-16</title>
    <author>
        <name>huiles de poisson</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi,<br />
jQuery makes event pooling simple through the use of jQuery.bind() and jQuery.trigger().<br />
This is the good step towards building an advanced Javascript Application since we can now bind methods to unique location.hash’s.<br />
<a href="http://www.vitabits.fr/huiles-de-poisson">huiles de poisson</a><br />
</p>]]>
    </content>
    <published>2009-12-17T07:45:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2231091</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2231091" />
    <title>Comment from Glen on 2009-12-17</title>
    <author>
        <name>Glen</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Isn't there a third choice ... delaying setting the property until you are truly ready for the property to be set?</p>

<p>(using the Flex Life Cycle methods, instead of creating your own life cycle)</p>

<p>It would seem that you could do your on-going logic by modifying a local var.  Then, when you are finished with your logic, you could call invalidateProperties().</p>

<p>Your commitProperties() method could do the actual setting of the selectedIndex property.</p>]]>
    </content>
    <published>2009-12-17T15:21:34Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2283044</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2283044" />
    <title>Comment from Frank on 2010-01-20</title>
    <author>
        <name>Frank</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hi,<br />
As huiles de poisson says it is possible to do this in a robust manner using jQuery, On my <a href="http://www.packyourbags.com/Holidays-in-Greece.aspx">holidays to greece</a> I met a guy who showed me how to do this, as i'm not the best using jQuery.</p>]]>
    </content>
    <published>2010-01-20T11:47:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2308964</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2308964" />
    <title>Comment from Kate on 2010-02-02</title>
    <author>
        <name>Kate</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Nice post, keep up the good work. You could probably use a similar technique to let you use event pooling to improve performance. If you are in the market for <a href="http://www.computertodays.org/">computertodays</a>, here's the guide that you've been looking for. </p>]]>
    </content>
    <published>2010-02-03T01:00:24Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2400801</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2400801" />
    <title>Comment from Alex on 2010-03-25</title>
    <author>
        <name>Alex</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Great programming tutorial, however i don't actually understand it, but do you want to get <a href="http://bali-vacations-indonesia.blogspot.com/">bali package holidays</a>?</p>]]>
    </content>
    <published>2010-03-26T02:41:56Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2400817</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2400817" />
    <title>Comment from Sumi on 2010-03-25</title>
    <author>
        <name>Sumi</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Actually i'm so confuse with Flex script above..<br />
<a href="http://rc-drift-cars.co.cc">rc drift cars</a></p>]]>
    </content>
    <published>2010-03-26T02:50:27Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2401506</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2401506" />
    <title>Comment from Quatro on 2010-03-26</title>
    <author>
        <name>Quatro</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thanks for the EventManager class.  The explanation is tempting me to use this although I do wonder whether this is necessary.</p>

<p>The danger with turning off notifications is that they are not re enabled for some reason.</p>

<p>Quatro from <a href="http://www.spotlighted.co.uk">walton on thames</a></p>]]>
    </content>
    <published>2010-03-26T15:23:16Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35664-comment:2403333</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35664" type="text/html" href="http://insideria.com/2009/12/disabling-events.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/12/disabling-events.html#comment-2403333" />
    <title>Comment from David on 2010-03-28</title>
    <author>
        <name>David</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>The Flex script is absolute brilliant. Tanks a lot for this Event Manager. Hope there are many other tools like this in the future.</p>

<p>Greetings from a <a href="http://www.pianist-schieborn.de">Pianist</a> and a <a href="http://www.zauberer-zauberkuenstler-zaubern.de/Zauberer-Zauberkuenstler-Zauberei-Close-Up.html">Close-up Zauberer</a> from Germany.</p>]]>
    </content>
    <published>2010-03-28T08:38:07Z</published>
  </entry>

</feed
