
66
Chapter 4: Architecting Flex Applications
Using events to connect application functionality
ActionScript code executes asynchronously. This means that code in a Flex application continues
executing without being blocked by the execution of other code. However, you need a way to deal
with dependencies between the execution of different pieces of code in an application. There are
times when you do not want a function to execute until some other function has already executed.
You can use event handling for these dependencies.
For example, you might have one web service operation that uses data that another operation
returns. A result event is triggered when a web service operation successfully returns a result. You
can write a function that calls the second operation with the result of the first operation, and then
assign that function as the result event handler of the first operation, as the following example
shows. You can use this technique for any code that depends on the execution of other code.
...
<mx:WebService id="myService"...>
<mx:operation name="getFoo" result="myFooResultHandler(event.result)" />
<mx:operation name="getBarWithFooInput" />
</mx:WebService>
<mx:Script>
<![CDATA[
function myFooResultHandler(foo) {
myService.getBarWithFooInput(foo);
}
]]>
</mx:Script>
...
For more information about event handling, see Chapter 16, “Working with ActionScript in
Flex,” in
Developing Flex Applications
.
Using events to handle errors
Just as you can use events to connect application functionality, you can also use them to handle
errors. Flex data services contain a fault event that is triggered when a data service has a problem
returning result data. You can write an ActionScript event handler function that responds to fault
events. The following example shows how you specify a fault event handler for a web service
operation:
<mx:Script>
<![CDATA[
function showErrorDialog(error){
// Function implementation...
}
]]>
</mx:Script>
...
<mx:WebService id="WeatherService" ...">
<mx:operation name="getFoo"
fault="showErrorDialog(event.faultString);"
/
>
</mx:WebService>
Содержание FLEX-GETTING STARTED WITH FLEX
Страница 1: ...Getting Started with Flex...
Страница 4: ...4 Contents...
Страница 22: ...22 Chapter 1 Introducing Flex...