Determining if an item is listening for a specific event

by Peter deHaan on August 20, 2008

in ActionScript

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.

According to the Flex 3 documentation for the EventDispatcher class’s hasEventListener() method (link):

The difference between hasEventListener() and willTrigger() is that hasEventListener() examines only the object to which it belongs, whereas willTrigger() examines the entire event flow for the event specified by the type parameter.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/20/determining-if-an-item-is-listening-for-a-specific-event/ -->
<mx:Application name="Button_willTrigger_buttonDown_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![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);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Verify listeners"
                click="verify_click();" />

        <mx:Spacer width="100%" />

        <mx:Button label="addEventListener()"
                click="addEventListener_click();" />
        <mx:Button label="removeEventListener()"
                click="removeEventListener_click();" />
    </mx:ApplicationControlBar>

    <mx:Button id="btn" label="Button" />

</mx:Application>

View source is enabled in the following example.

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: