----- Original Message ----- 
From: "Dan" <[email protected]>
To: <[email protected]>
Sent: Monday, January 12, 2009 6:24 PM
Subject: [flexcoders] How to create universal event handler?


>I have class MyEvent extends flash.events.Event.
> I also have a second class MyEventChild extends MyEvent.
> Both classes define string constants that are used as event type.
>
> How can I add event listener to handle any of MyEvent or MyChildEvent
> events?
>
> If I do addEventListener(MyEvent.Evt1, handleEvt), I will handle only
> that specific  MyEvent.Evt1 event. I need to be able to handle
> MyEvent.Evt2 and MyChildEvent.Evt1 and my FutureChildEvent.EvtX.
> How can this be achieved?

Is this really a good route to go down?

Your universal event handler will turn into a big switch statement and will 
be responsible for handling  whatever is thrown at it.

The difference between having separate events (most likely custom events) is 
that the handler method is invoked in the correct context for the event and 
that handler method only needs access to data structures that need to be 
changed for that specific event. The event and handler only change in 
response to changing circumstances regarding that specific event (in the 
future it may need to carry some extra or different data).

If you have a universal event handler, the handler function has to have 
access to all the data structures to be manipulated in response for all the 
'subevents'. The universal handler must be changed every time an event is 
added, or changed.

It seems as though the universal handler scheme just avoids the creation of 
individual events and handlers in favour of putting that same effort in one 
basket rather than have the flexibility of separate true events. Effectively 
you are building a controller mechanism  inside of a single event handler.

Paul

>
> Thank you! 

Reply via email to