BTA Flexing

My passion RIAs…………..Shardul Singh Bartwal

Managing Custom Events in Flex 3.0 in a easy way

Posted by Shardul Singh Bartwal on April 15, 2009

As all of us require Custom Events in our Flex applications. Also we know the Event Life Cycle in Flex. I am sure that all of you will be familiar with event targating,bubling,capturing etc.Also we know that we are require to remove the Listeners which we adds. We all know that inside a flex application the event first goes to the application level and then from there back to the component which have to listen it. This issue becomes more critical if we are not using any particular architecture like Pure MVC or Cairngorm etc.So in this condition first you have to dispatch a event and listen it at application level and then again dispatch a event on its occurring and listen that at the desired place. The other ways are like by using the references of the Class from where the event is dispatched and where it is being listen in each other etc.I have created a CustomEventManager Class for managing custom event dispatching, and Listening it.Its very simple to use it. It is a static class.

For dispatching event simply write like this:

CustomEventManager.dispatchEvent(new YourCustomEvent(YourCustomEvent.YOUR_CUSTOM_EVENT));

and for Listioning the custom event write like this at right place in the event Listioning
class(e.g. at creationComplete of the class or constructor etc ):

CustomEventManager.addEventListener(YourCustomEvent.YOUR_CUSTOM_EVENT,yourCustomEventHandler);

I am also adding the code of the ‘CustomEventManager’ class.Hope it will be helpful for you.

For CustomEventManager Class click here.

Advertisements

6 Responses to “Managing Custom Events in Flex 3.0 in a easy way”

  1. shaun said

    It’s a good idea to have a single EventDispatcher for an application or module, but in my opinion, it’s not the best idea to create a “central” EventDispatcher: ie a Singleton EventDispatcher accessed by static methods. Problems arise when later you decide that you want two or more applications or modules running side-by-side in the same SWF – they now share the same dispatcher and events overlap or clash. An application or module should have it’s own EventDispatcher, not shared by other applications or modules.

    A workaround might be to use the Multiton approach – something like:

    CustomEventManager.use(‘key1’).dispatchEvent()

    Different string keys return different EventDispatchers. If one doesn’t exist perhaps it gets created. A single application uses the same key everywhere. A different application/module uses a different key. Just a thought 🙂

  2. Jack said

    Thanks for this suggestion. I’ve been struggling with creating an all-encompassing event structure for my multi-modular application in which modules must communicate with one another when certain custom events are fired. So far, it’s been slow going trying to get listener functions in one module to get called when I dispatch an event in another module. All works well at the Application level, but as soon as it dips below the modular level (both on the listener and dispatcher ends), communication falls apart. It’s almost like there’s a barrier in the event flow between externally-loaded modules and the parent application.

    Anyway…I’ll give this a try. Thanks again!

    • Cynthia said

      @Jack:

      Did you ever figure this out? I am currently wanting to dispatch an event from one module that will get listened for by another module and I can’t get it to work… I’m curious if anyone else has gotten that scenario to work? It seems odd that the parent app should have to know about what events the modules want to dispatch or listen for in order for the modules to know about them…

  3. shardul said

    ok………..thnx for the feed back I will also try it for the multi moduler application as soon as possible.

  4. Darrin said

    I am trying to figure this out in a cairngorm framework, can you post some sample code, with any imports and what not. Thanks

  5. Shardul Singh Bartwal said

    It works fine with the modules too,pls check both of ur modules are loaded………….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: