We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Using custom events independent from UIComponent

spiritwithin
spiritwithin asked
on
Medium Priority
583 Views
Last Modified: 2013-11-11
Hello,

pretty straightforward: if i have a custom class that stands alone (no subclass of any other classes, specifically no subclass of UIComponent) - is there any way i can integrate custom event handlers? How can i write my own events, hook them to functions and integrate all of this in a custom class?

Please note that i want to use objects that have no represantation in the view (or user interface) of my application!

Any help would be appreciated!

Thank you
Comment
Watch Question

Sr. Software engineer
CERTIFIED EXPERT
Commented:
>> if i have a custom class that stands alone is there any way i can integrate custom event handlers
As long as your class extends EventDispatcher

I quote the Flex help:
In general, the easiest way for a user-defined class to gain event dispatching capabilities is to extend EventDispatcher.
If this is impossible (that is, if the class is already extending another class), you can instead implement the IEventDispatcher interface, create an EventDispatcher member, and write simple hooks to route calls into the aggregated EventDispatcher.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:

Commented:
Dispatch event is actionscript example:

var eventObject:Object = new Object;
eventObject.name = "newSelected";
var event:myCustomEvent= new myCustomEvent(eventObject, "newSelected");
dispatchEvent(event);

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
An example (+usage) of the most simple custom event you can have:

package {
 
      import flash.events.Event
 
      public class CustomEvent extends Event{
 
            public static const CEVENT01:String = "cEvent01"   // <<< Choose your own convenient names here
            public static const CEVENT02:String = "cEvent02"
            public static const CEVENT03:String = "cEvent03"
            public static const CEVENT04:String = "cEvent04"
 
            public function CustomEvent(customEventString:String){
                   super(customEventString, true, false)
             }
      }
}

Usage:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" 
	creationComplete="init()">
 
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert
			import CustomEvent
 
			public function init():void{
				//Add the listeners for the events
				addEventListener(CustomEvent.CEVENT01, launchEvent01)
				addEventListener(CustomEvent.CEVENT02, launchEvent02)
				addEventListener(CustomEvent.CEVENT03, launchEvent03)
				addEventListener(CustomEvent.CEVENT04, launchEvent04)
			}
 
 			public function launchEvent01(e:CustomEvent):void{
				Alert.show("Custom Event - 1 was thrown.")
			}
			public function launchEvent02(e:CustomEvent):void{
				Alert.show("Custom Event - 2 was thrown.")
			}
			public function launchEvent03(e:CustomEvent):void{
				Alert.show("Custom Event - 3 was thrown.")
			}
			public function launchEvent04(e:CustomEvent):void{
				Alert.show("Custom Event - 4 was thrown.")
			}
 
		]]>
	</mx:Script>
	<mx:VBox>
		<mx:Button id="btn01" label="Event 1" click="dispatchEvent(new CustomEvent(CustomEvent.CEVENT01))"/>
		<mx:Button id="btn02" label="Event 2" click="dispatchEvent(new CustomEvent(CustomEvent.CEVENT02))"/>
		<mx:Button id="btn03" label="Event 3" click="dispatchEvent(new CustomEvent(CustomEvent.CEVENT03))"/>
		<mx:Button id="btn04" label="Event 4" click="dispatchEvent(new CustomEvent(CustomEvent.CEVENT04))"/>
	</mx:VBox>
 
</mx:Application>

Open in new window

zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanx 4 axxepting
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.