Handling events
Subtopics:
What you need to know
With Flex events, you use handler functions to respond to both asynchronous network and user generated events.
- PHP KEY CONCEPT: Asynchronous events might be a new concept for many PHP developers. We highly recommend reviewing this section. In PHP, there really aren't any events. Event is basically the user requesting a web page. If you're familiar with Javascript though, event handling is similar.
- ASP Event handling is similar to the way events are handled via the callback function and code-behind concepts.
Use Event Listeners (also called Event Handlers) to respond to events
Events are generated in three ways: User actions (e.g. mouse click), Network (e.g. data received), and the Flex Application itself (for example, page loaded event).
When an event is dispatched, you may want your Flex application to respond. This is done via an event listener or handler function that is called when certain events are fired.
There are three phases to the Flex event flow mechanism: capturing, targeting, and bubbling. |
rate-5701651-12356
| User rating? |
|
|
|
|
SAMPLE CODE OVERVIEW: Handling an application event (asynchronous network event)
The following application:
- Uses an HTTPService control that retrieves a twitter RSS feed.
- Responds to the asynchronous event using a handler function.
- Makes the remote call in response to a user generated click event.
The finished application will look like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http:
width="500" height="500">
<mx:HTTPService
id="twitterService"
url="http:
resultFormat="e4x"
result="twitterServiceResultHandler(event);"
/>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
private function twitterServiceResultHandler(event:ResultEvent):void
{
resultTxt.text = event.result.toString();
}
]]>
</mx:Script>
<mx:Button id="myButton" label="Send HTTP Request"
click="twitterService.send()"/>
<mx:TextArea id="resultTxt" width="100%" height="100%"/>
</mx:Application>
Note that this code requires that Twitter provide a crossdomain.xml file (also called a crossdomain policy file) that enable access from your application. For more information on crossdomain policy files, see Programming ActionScript 3.0 and the Flex deployment checklist.