ActionScript in Flex Applications - Nested ActionScript (Page 3 of 4 )
You also can nest ActionScript code within MXML tags. Just as you can nest values for most properties you can nest the values (ActionScript) for event handlers. You must place the code within a CDATA block. Here’s an example:
The second way to add ActionScript code to an application is to place it within an MXML script. An MXML script appears in an MXML document within a Script element:
<mx:Script> </mx:Script>
Since ActionScript code may use special characters otherwise interpreted by the MXML compiler, you must place ActionScript code withinScripttags and also within aCDATA block, as in the following example:
<mx:Script> <![CDATA[
import mx.controls.Alert;
private function example():void { Alert.show("Example"); }
]]> </mx:Script>
You can optionally place ActionScript code blocks in separate files, and you can embed them in a script block using thesource attribute of aScripttag:
Classes are the most sophisticated and powerful use of ActionScript. Although it’s not wrong to use inline code and MXML scripts, it’s generally advisable to place the majority of ActionScript code within ActionScript classes.
ActionScript class code exists within separate documents, apart from the MXML application and component documents. ActionScript class files are text files that use the file extension .as. We’ll talk more about creating classes later in this chapter, in the “Declaring Classes” section.