How to handle custom EventDispatcher events

trudyhlittle
trudyhlittle used Ask the Experts™
on
I am trying to test a simple EventDispatcher application.  I am registering the event handler with the addEventListener event then manually triggering the event using dispatchEvent, but my event handlers are not being called.  Thanks.

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()" width="100%" >

	<mx:Style source="assets/skins/skins.css"/>

	<mx:Script>
		<![CDATA[
		
			import mx.controls.Alert;
			import events.*;
			
			private var contextMenuEventManager:ContextMenuEventManager;
			
			private function init():void {
			 	contextMenuEventManager = ContextMenuEventManager.getInstance();
			 	
			 	contextMenuEventManager.addEventListener(EnableContextMenuEvent.ENABLECONTEXTMENU, handleEnableContextMenuEvent);
				contextMenuEventManager.addEventListener(DisableContextMenuEvent.DISABLECONTEXTMENU, handleDisableContextMenuEvent);
 
            }
			
			public function handleEnableContextMenuEvent(event:EnableContextMenuEvent):void {  
				Alert.show(event.menuText, "Enabled Caught");
			}
			
			public function handleDisableContextMenuEvent(event:DisableContextMenuEvent):void {  
				Alert.show(event.menuText, "Disable Caught");
			}
			
			  private function enableContextMenuButtons():void 
            {
            	
            	var eEnable:EnableContextMenuEvent;
            	eEnable = new EnableContextMenuEvent(EnableContextMenuEvent.ENABLECONTEXTMENU, 
            	 "Delete Selected", true, true);
			   	contextMenuEventManager.dispatchEvent(eEnable);
			   	eEnable = new EnableContextMenuEvent(EnableContextMenuEvent.ENABLECONTEXTMENU, 
            	 "Resend Selected", true, true);
			   	contextMenuEventManager.dispatchEvent(eEnable);
			   	Alert.show("enableContextMenuButtons", "enableContextMenuButtons");
            }
            
            private function disableContextMenuButtons():void 
            {
            	var eDisable:DisableContextMenuEvent;
            	eDisable = new DisableContextMenuEvent(DisableContextMenuEvent.DISABLECONTEXTMENU, 
            	 "Delete Selected");
			   	contextMenuEventManager.dispatchEvent(eDisable);
			   	eDisable = new DisableContextMenuEvent(DisableContextMenuEvent.DISABLECONTEXTMENU,
            	 "Resend Selected");
			   	contextMenuEventManager.dispatchEvent(eDisable);
			   	Alert.show("disableContextMenuButtons", "disableContextMenuButtons");
            }

		]]>
	</mx:Script>
	

<mx:Canvas  width="100%" height="100%"  >
		<mx:Label text="Test Events" styleName="panelTitle" x="10" y="10"/>

			   <mx:HBox x="10" y="40">
			      <mx:Button id="eButton" label="Enable" click="enableContextMenuButtons();" />
			      <mx:Button id="dButton" label="Disable" click="disableContextMenuButtons;"  />
			   </mx:HBox>
</mx:Canvas>

</mx:Application>

package events
{
	import flash.events.Event;

	public class DisableContextMenuEvent extends Event
	{
		public function DisableContextMenuEvent(type:String, menuText:String, bubbles:Boolean=false,
		 cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
			this.menuText = menuText;
		}
		
		public static const DISABLECONTEXTMENU:String = 'DisableContextMenuEvent';
		
		public var menuText:String;
		
	}
}

package events
{
	import flash.events.Event;

	public class EnableContextMenuEvent extends Event
	{
		public function EnableContextMenuEvent(eventType:String, menuText:String, 
		bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
			this.menuText = menuText;
		}
		
		public static const ENABLECONTEXTMENU:String = 'EnableContextMenuEvent';
		
		 public var menuText:String;
         
		
	}
}

package events
{
	import flash.events.EventDispatcher;

	public class ContextMenuEventManager extends EventDispatcher
	{
		// Singleton
	    private static var eventManager:ContextMenuEventManager;
	
	    public static function getInstance():ContextMenuEventManager {
	        if (eventManager == null) eventManager = new ContextMenuEventManager(arguments.callee);
	        return eventManager;
	    }

		public function ContextMenuEventManager(caller:Function =null)
		{
			if (caller != ContextMenuEventManager.getInstance) {
	            throw new Error("ContextMenuEventManager is a singleton class, please use getInstance().")
	        }
	        if (ContextMenuEventManager.eventManager != null) {
	            throw new Error("Only one instance of ContextMenuEventManager is allowed.")
	        }

		}
		
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There are small missouts:

1. EnableContextMenuEvent.as
-------------------------------------------
in the constructor:

public function EnableContextMenuEvent(eventType:String, menuText:String,
            bubbles:Boolean=false, cancelable:Boolean=false)
            {
                  // super(type, bubbles, cancelable); // "type" parameter name should be "eventType" as received in constructor parameter
                        super.(eventType, bubbles, cancelable);
                        this.menuText = menuText;
            }
----------------------

2. in MXML

make the click function call properly

//wrong way, function call should have parenthesis
<mx:Button id="dButton" label="Disable" click="disableContextMenuButtons;"  />

//correct way
<mx:Button id="dButton" label="Disable" click="disableContextMenuButtons();"  />
------------------

That's it to make it work.

Still I have one more thing to address:

in enableContextMenuButtons() method you have created events to be dispatch, there you are using "bubbles" parameter "true" (The third parameter)

eEnable = new EnableContextMenuEvent(EnableContextMenuEvent.ENABLECONTEXTMENU,
                   "Delete Selected", true, true);

It's used to bubble the event in case you want the parent of dispatcher to listen the event instead of the actual dispatcher. In this case it should be false.

You can learn more about event bubble at: http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

Author

Commented:
Excellent.  Thanks.  I knew I must be doing something stupid.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial