In series of posts I will try to bring you closer to what you can hear during your Flex developer interview. You will hear most of them, depending on where are you trying to get a job and who will question you. I will try to present you topics from basic Actionscript to a bit more advanced Flex issues.
Although this question is more about Actionscript, you will hear it at 99% of Flex developer interviews. Ok, let’s get to the point.
Event system in Actionscript consists of three elements: dispatchers, listeners and event objects. Event dispatcher is the subject of the event, event is the event object encapsulating details of the event occurrence (can pass additional data to listeners) while listener is the handler function for dealing with an event.
If you are familiar with Java design patterns this is also known as Observer Pattern. In Java nomenclature these are known as subjects, observers and event objects. When names are different, the idea stays the same.
Key for dispatching events and notifying listeners is the EventDispatcher class. Functionality of that class covers the following (methods defined by IEventDispatcher interface):
- addEventListener – for registering listener objects,
- dispatchEvent – dispatches events,
- hasEventListener – check whether the EventListener has any listeners defined by the parameter,
- removeEventListener – removes listener from an EventDispatcher object,
- willTrigger – checks whether an event listener is registered with EventDispatcher.
You can use EventDispatcher by implementing its methods in your class or – more common approach – by extending EventDispatcher class.
If the user interacts with the application, dispatching an event like clicking a button will affect many different objects. This happens because Actionscript 3.0 introduced event propagation – a mechanism of propagating events down from a stage through all objects to their children up to our button in this example.
Events starts with capturing phase, starting from top most parent object – stage. During this phase all objects, down to the one where event originated, are notified of the event. Difference is that event object has event.eventPhase property set to EventPhase.CAPTURING_PHASE.
Than event phase is changed to targeting phase. This phase means that event is at the target object or the object from which event originated. This phase relates always to only one – that object.
After reaching target object, event “goes back” through all the parents again. This phase is called the bubbling phase.
What can be added? Listening to capture phase is pretty rare. In most cases, you will use the following:
object.addEventListener( “SomeEvent”, eventListenerFunction );
This is the default behavior. It means event will be listened at target and bubbling phases. To listen event at capture phase, add a third parameter like in example:
object.addEventListener( “SomeEvent”, eventListenerFunction, true );
For more information, you may want to use the following: http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
Latest Comments