View Full Version : Prevent superclass dispatching event
arkum
10-05-2008, 12:51 PM
Hi,
I am subclassing a Flex Component. In one method it dispatches an Event.CHANGE event. I need to alter some of the component's values Before this event is dispatched.
I can't override the whole function as it refers to private vars/methods in the superclass.
If I override and call super.myMethod() first, the evnt is dispatched before I can alter the values.
If I override and call super.myMethod() after my code, the values I need to alter are reset.
Is there any way I can supress the event in the superclass, then send the event again in my overriden method?
drkstr
10-05-2008, 11:14 PM
That's a tricky one. I'm not sure what your end goal is, but maybe you can dispatch your own event after you make the updates, and act on that event instead of Event.CHANGE.
Best Regards,
~Aaron
*EDIT*
If that doesn't work, you could also probably attach a listener for the Event.CHANGE before you call super.myMethod(). Then in the event handler, you can event.preventDefault() + event.stopImidiatePropegation(), remove the listener, and make any needed updates. Dispatch Event.CHANGE again when you're done.
arkum
10-06-2008, 10:43 AM
Ahh.
Of course. Listen to it myself from within the class and supress the event, then fire it again and let it pass.
Thank you for your reply. That was most helpful.
senocular
10-06-2008, 03:34 PM
be sure you set the listener in your constructor, and give it a priority of int.MAX_VALUE
arkum
10-06-2008, 04:09 PM
But surely I'll need to add / remove / add?
1. addEventListener (Using MAX_VALUE)
2. catch the event and stop it from propagating
3. removeEventListener (So that the ammended event isn't trapped when dispatched)
3. dispatch ammended Event
4. addEventListener (Using MAX_VALUE)
Obviously I could set my Ammended event to be uncancelable, but if I don't remove the listener I will end up in a loop.
senocular
10-06-2008, 04:17 PM
if you remove then add again, you allow the possibility (though ever so slight) that someone else might add a max value listener that will be called before yours is (order in which the listener was added will be used to determine precedence when there are matching priorities).
You would need to detect that second event, your event, and let it go through instead of stopping it like the event from the superclass.
arkum
10-06-2008, 04:24 PM
Mmm.
Thanks.
But how to detect the second event? Subclass the event type and check if is SubclassedEvent (Therefore don't cancel)?
Is this ok - The Event type could still be the same...
drkstr
10-06-2008, 07:29 PM
But how to detect the second event? Subclass the event type and check if is SubclassedEvent (Therefore don't cancel)? That should work. Attach the listener in the constructor like suggested. Then if(event is CustomEvent) let it go through, otherwise suppress and dispatch CustomEvent. This will guarantee all default change events are suppressed while your event will go through.
Best Regards,
~Aaron
arkum
10-07-2008, 11:11 AM
Thanks guys.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.