[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? Thank you!
Re: [flexcoders] How to create universal event handler?
Create a custom event that uses your current event type string as a variable. Then your handler can look at the variable value to decide what to do. On Mon, Jan 12, 2009 at 1:24 PM, Dan danijel.arsenov...@gmail.com wrote: 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? -- Howard Fore, howard.f...@hofo.com The universe tends toward maximum irony. Don't push it. - Jeff Atwood
Re: [flexcoders] How to create universal event handler?
On Mon, Jan 12, 2009 at 11:54 PM, Dan danijel.arsenov...@gmail.com wrote: 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? You have to add a listener for each event type separately. obj.addEventListener(foo, commonHandler); obj.addEventListener(bar, commonHandler); obj.addEventListener(baz, commonHandler); ... There's no shortcut way to listen for all events from an object. Manish
Re: [flexcoders] How to create universal event handler?
- Original Message - From: Dan danijel.arsenov...@gmail.com To: flexcoders@yahoogroups.com 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!