Flex 3 Event Handling …
This is a topic that EVERY software professional (developing Flex/Flash) should be familiar with. Not to say that you should know every little detail about it, but helps to know the when, how and what is triggering your events and what you can expect from them.
If you want the gory details, here’s the link to Adobe. There! Now, on with the code.
Let’s start out with something simple. Very simple.
What is an event? How do they happen?
An event is purely and simple something that happens; A click of a button, the refreshing of a page, the focus on a text field, etc, etc.
First, by listening to what’s happening. For every action there’s a reaction. If you want to throw a ball, you want someone to catch it. You (the event dispatcher), the catcher (the event listener).
We can get very detailed about the event flow and how it propagates itself on the DOM of Flash/Flex. Suffice it to say that it all starts at the Parent Node (the Application/MXML) and travels all the way down to the object (broadcasting/sending) the event.
Adding an Event
An event can be triggered in 2 ways:
1. MXML
<mx:button ... click="callFunctionOnClickEvent(event);"/>
CLICK = Event Handler
2. ACTIONSCRIPT 3
myButton.addEventHandler(MouseEvent.CLICK, myFuctionToBeCalledOnClick);
This is pure code, so we’re responsible for creating a button, giving it its properties (look and feel) and ultimately telling it what to do in case someone presses on it. As you can see, we attach the Event Listener to the button itself.
REMEMBER: In real-life scenarios, applications with multiple layers will dispatch and listen for events at different levels. It is very important to understand the ‘bubbling‘ of said events. We’ll get into that later…
Here’s what it looks like all put together:
Some source code for that azz!
Leave a Comment