Flex Custom Events - Part I

This is one of those areas that a lot of Flex developers I've talked to tend to avoid. The idea behind a custom event is that you create them inside your components so that you can broadcast the event up to the main application file of your Flex app. Why? The main reason is so that you can de-couple your components by putting the event-handling logic somewhere outside of the component. This makes your component easier to test and more re-usable. Finally, it is one way to implement a Front Controller/Command pattern (more on that in a future post).

Creating a custom event and then broadcasting it requires several steps. Read more...

Step 1: In the component, declare the name (type) of the custom event:

<mx:Metadata>
   [ Event( name="myCustomEvent", type="flash.events.Event") ]
</mx:Metadata>

Notice that you can call your event whatever you want, though you cannot override an event already defined in the Flex API. So instead of an event called "click", your application can respond to something more indicative like "addProduct" or "registerUser".

Step 2: In response to some user event, like a button click, call a function that creates the object and then dispatches it. Note that we're still inside the component at this point.

private function onButtonClick():void {
   
   // create your new event object
   var myEventObj:Event = new Event( "myCustomEvent" );

   // broadcast the event to whomever may be listening
   dispatchEvent( myEventObj );
}

Step 3: In the parent of the component, add the event listener to the invocation of the custom component.

<comp:MyComponent id="myComponent" myCustomEvent="onMyCustomEvent( event );" />

Step 4: Write the custom event handler in the parent component. In this case the handler would look something like this:

private function onMyCustomEvent( event:Event ):void {
   mx.controls.Alert.show('I heard that!');
}

In a later post I'll describe a slightly more in-depth example, whereby you can add a data payload to the event before broadcasting it.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Fernando Madruga's Gravatar Funny how there are over 3000 views of this page and not a single comment! :)

I'd like to thank you for such a clear and concise way of explaining this topic: I was a bit lost in the standard docs until I googled and came here!
# Posted By Fernando Madruga | 4/28/08 6:05 AM
Srikanth's Gravatar There are a few changes in the onButtonClick() method.

import flash.event.Event;
private function onButtonClick():void {
// create your new event object
var myEventObj:Event = new Event( "myCustomEvent" );
   
// broadcast the event to whomever may be listening
dispatchEvent( myEventObj );
}
# Posted By Srikanth | 5/1/08 6:00 AM
Senthil's Gravatar Can you give idea when we adding a component dynamically. Like add a tab to a navigator with component dynamically. During this issue how you will create a broadcasting event and how it will communicate with main App?
# Posted By Senthil | 8/14/08 11:45 PM
Wally Kolcz's Gravatar If you were to have a TitleWindow component as a pop up that has a form that adds a record to a database. When you complete the adding, but before you close the pop-up, you want to dispatch the event 'somethingAdded' and trigger the datagrid on the parent to reload.

How can you add the listener to the parent to hear it when you are using the PopUpManager to spawn the TitleWindow? Your step 3 is if there is an included component. Thanks!
# Posted By Wally Kolcz | 9/4/08 7:38 AM
Wally Kolcz's Gravatar Where do you declare or import 'CustomEventEvent'? I am getting and error saying 'Call to possible undefined method CustomEventEvent'.
# Posted By Wally Kolcz | 9/4/08 8:50 AM
Oliver Merk's Gravatar Sorry, the line in Step 2 should read:

var myEventObj:Event = new Event( "myCustomEvent" );

Oliver
# Posted By Oliver Merk | 9/4/08 7:33 PM
numerous6's Gravatar You dont need to inlclude the meta information, it works without it.
# Posted By numerous6 | 12/11/08 12:52 PM
Jeffrey's Gravatar Question:
Does this only work in a separate component file? What if I have everything in main file?
# Posted By Jeffrey | 2/18/09 6:11 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.