• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

Using custom events independent from UIComponent

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
0
spiritwithin
Asked:
spiritwithin
  • 4
1 Solution
 
zzynxSoftware engineerCommented:
>> 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.
0
 
Jones911Commented:
Dispatch event is actionscript example:

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

0
 
zzynxSoftware engineerCommented:
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

0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now