<?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>Flex Examples &#187; addEventListener()</title>
	<atom:link href="http://blog.flexexamples.com/tag/addeventlistener/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dispatching custom events from a custom component in Flex 4</title>
		<link>http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/</link>
		<comments>http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 02:07:36 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[Event]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[addEventListener()]]></category>
		<category><![CDATA[dispatchEvent()]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[metadata]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/?p=3388</guid>
		<description><![CDATA[<p>The following example shows how you can declare custom events in an MXML or ActionScript component in Flex 4 by specifying the [Event] metadata.</p> <p></p> &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;!-- http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/ --&#62; &#60;s:Application name=&#34;Spark_Event_test&#34; xmlns:fx=&#34;http://ns.adobe.com/mxml/2009&#34; xmlns:s=&#34;library://ns.adobe.com/flex/spark&#34; xmlns:mx=&#34;library://ns.adobe.com/flex/mx&#34; xmlns:comps=&#34;comps.*&#34;&#62; &#160; &#60;fx:Script&#62; &#60;!&#91;CDATA&#91; import mx.controls.Alert; &#160; protected function btn_panicHandler&#40;evt:Event&#41;:void &#123; Alert.show&#40;&#34;Oh noes! I has an error!!1!&#34;, evt.currentTarget.label&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can declare custom events in an MXML or ActionScript component in Flex 4 by specifying the [Event] metadata.</p>
<p><span id="more-3388"></span></p>
<p class="alert">The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see <a href="http://bit.ly/crThlI">http://www.adobe.com/products/flex/</a>. To download the latest nightly build of the Flex 4 SDK, see <a href="http://bit.ly/Flex4SDK">http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4</a>.<br/><strong>For more information on getting started with Flex 4 and Flash Builder 4, see the official <a href="http://bit.ly/dCkecm">Adobe Flex Team blog</a>.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> name=<span style="color: #ff0000;">&quot;Spark_Event_test&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">        xmlns:comps=<span style="color: #ff0000;">&quot;comps.*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            import mx.controls.Alert;</span>
&nbsp;
<span style="color: #000000;">            protected function btn_panicHandler<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                Alert.show<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Oh noes! I has an error!!1!&quot;</span>, evt.currentTarget.label<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HGroup</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;comps:PanicButtonMXML</span> id=<span style="color: #ff0000;">&quot;panicMXML&quot;</span></span>
<span style="color: #000000;">                label=<span style="color: #ff0000;">&quot;MXML&quot;</span></span>
<span style="color: #000000;">                height=<span style="color: #ff0000;">&quot;60&quot;</span></span>
<span style="color: #000000;">                panic=<span style="color: #ff0000;">&quot;btn_panicHandler(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;comps:PanicButtonAS</span> id=<span style="color: #ff0000;">&quot;panicAS&quot;</span></span>
<span style="color: #000000;">                label=<span style="color: #ff0000;">&quot;ActionScript&quot;</span></span>
<span style="color: #000000;">                height=<span style="color: #ff0000;">&quot;60&quot;</span></span>
<span style="color: #000000;">                panic=<span style="color: #ff0000;">&quot;btn_panicHandler(event);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:HGroup</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The custom PanicButtonMXML component, <em>comps/PanicButtonMXML.mxml</em>, is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> name=<span style="color: #ff0000;">&quot;PanicButtonMXML&quot;</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">        chromeColor=<span style="color: #ff0000;">&quot;red&quot;</span></span>
<span style="color: #000000;">        click=<span style="color: #ff0000;">&quot;doPanic(event);&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
        [Event(&quot;panic&quot;)]
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            public static const PANIC:String = <span style="color: #ff0000;">&quot;panic&quot;</span>;</span>
&nbsp;
<span style="color: #000000;">            protected function doPanic<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                dispatchEvent<span style="color: #66cc66;">&#40;</span>new Event<span style="color: #66cc66;">&#40;</span>PANIC<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Button</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The custom PanicButtonAS component, <em>comps/PanicButtonAS.as</em>, is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/** http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/ */</span>
<span style="color: #9900cc; font-weight: bold;">package</span> comps <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> spark<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>Button<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;panic&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PanicButtonAS <span style="color: #0033ff; font-weight: bold;">extends</span> Button <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> static const PANIC<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;panic&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PanicButtonAS<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #004993;">setStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;chromeColor&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;red&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> doPanic<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> doPanic<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span>PANIC<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p class="alert">This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Dispatching custom events from a custom component in Flex 4 on FlexExamples.com',url: 'http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/',contentID: 'post-3388',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'addEventListener(),dispatchEvent(),event,metadata',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2010/10/24/dispatching-custom-events-from-a-custom-component-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Determining if an item is listening for a specific event</title>
		<link>http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/</link>
		<comments>http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 05:56:33 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[addEventListener()]]></category>
		<category><![CDATA[hasEventListener()]]></category>
		<category><![CDATA[removeEventListener()]]></category>
		<category><![CDATA[willTrigger()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/</guid>
		<description><![CDATA[<p>The following example shows how you can check whether a Flex Button control is listening for a specific event (FlexEvent.BUTTON_DOWN) by using the hasEventListener() and willTrigger() methods.</p> <p>According to the Flex 3 documentation for the EventDispatcher class&#8217;s hasEventListener() method (<a href="http://livedocs.adobe.com/flex/3/langref/flash/events/EventDispatcher.html#hasEventListener()">link</a>):</p> <p> The difference between hasEventListener() and willTrigger() is that hasEventListener() examines only the object [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can check whether a Flex Button control is listening for a specific event (<code>FlexEvent.BUTTON_DOWN</code>) by using the <code>hasEventListener()</code> and <code>willTrigger()</code> methods.</p>
<p>According to the Flex 3 documentation for the EventDispatcher class&#8217;s <code>hasEventListener()</code> method (<a href="http://livedocs.adobe.com/flex/3/langref/flash/events/EventDispatcher.html#hasEventListener()">link</a>):</p>
<blockquote><p>
The difference between <code>hasEventListener()</code> and <code>willTrigger()</code> is that <code>hasEventListener()</code> examines only the object to which it belongs, whereas <code>willTrigger()</code> examines the entire event flow for the event specified by the type parameter.
</p></blockquote>
<p>Full code after the jump.</p>
<p><span id="more-754"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Button_willTrigger_buttonDown_test/bin/srcview/source/main.mxml.html">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/ --&gt;
&lt;mx:Application name="Button_willTrigger_buttonDown_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.utils.StringUtil;

            private function verify_click():void {
                var listener:Boolean = btn.hasEventListener(FlexEvent.BUTTON_DOWN);
                var trigger:Boolean = btn.willTrigger(FlexEvent.BUTTON_DOWN);
                var str:String = "hasEventListener() = {0}{1}willTrigger() = {2}";
                Alert.show(StringUtil.substitute(str, listener, "\n", trigger));
            }

            private function addEventListener_click():void {
                btn.addEventListener(FlexEvent.BUTTON_DOWN, btn_buttonDown);
                verify_click();
            }

            private function removeEventListener_click():void {
                btn.removeEventListener(FlexEvent.BUTTON_DOWN, btn_buttonDown);
                verify_click();
            }

            private function btn_buttonDown(evt:FlexEvent):void {
                Alert.show(evt.type);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button label="Verify listeners"
                click="verify_click();" /&gt;

        &lt;mx:Spacer width="100%" /&gt;

        &lt;mx:Button label="addEventListener()"
                click="addEventListener_click();" /&gt;
        &lt;mx:Button label="removeEventListener()"
                click="removeEventListener_click();" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:Button id="btn" label="Button" /&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Button_willTrigger_buttonDown_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Button_willTrigger_buttonDown_test/bin/main.html" width="100%" height="200"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Determining if an item is listening for a specific event on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/',contentID: 'post-754',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'addEventListener(),hasEventListener(),removeEventListener(),willTrigger()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

