Using custom events independent from UIComponent

Posted on 2009-02-13
Last Modified: 2013-11-11

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
Question by:spiritwithin
    LVL 37

    Accepted Solution

    >> 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.
    LVL 37

    Expert Comment

    LVL 19

    Expert Comment

    Dispatch event is actionscript example:

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

    LVL 37

    Expert Comment

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

    package {
          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)

    <?xml version="1.0" encoding="utf-8"?>
    			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{
   "Custom Event - 1 was thrown.")
    			public function launchEvent02(e:CustomEvent):void{
   "Custom Event - 2 was thrown.")
    			public function launchEvent03(e:CustomEvent):void{
   "Custom Event - 3 was thrown.")
    			public function launchEvent04(e:CustomEvent):void{
   "Custom Event - 4 was thrown.")
    		<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))"/>

    Open in new window

    LVL 37

    Expert Comment

    Thanx 4 axxepting

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
    The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now