Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to handle custom EventDispatcher events

Avatar of trudyhlittle
trudyhlittleFlag for United States of America asked on
Adobe FlashApache Flex
2 Comments1 Solution434 ViewsLast Modified:
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.")
	        }

		}
		
	}
}
ASKER CERTIFIED SOLUTION
Avatar of ActionScript_Helper
ActionScript_HelperFlag of India image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers