User Guide
Using ActionScript in Flex applications 49
Included ActionScript files do not need to be in the same directory as the MXML file. However,
you should organize your ActionScript files in a logical directory structure.
Flex detects changes in ActionScript files using timestamps. If the file has changed since the last
request, Flex regenerates the application before responding to the client. If you change the
ActionScript in one of the imported ActionScript files, the next time the application is requested,
the changes appear.
There are two ways to include an external ActionScript file in your Flex application:
• The source attribute of the <mx:Script> tag. This is the preferred method for including
external ActionScript class files.
• The #include directive inside <mx:Script> blocks.
The following sections describe these two methods of including an external ActionScript file.
Using the source attribute
You use the
source attribute of the <mx:Script> tag to include external ActionScript files in
your Flex applications. This provides a way to make your MXML files less cluttered and promotes
code reuse across different applications.
The following example shows the contents of the IncludedFile.as file:
public function computeSum(a:Number, b:Number):Number {
return a + b;
}
The following example imports the contents of the IncludedFile.as file. This file is located in the
same directory as the MXML file.
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script source="IncludedFile.as" />
<mx:TextInput id="ta1st" text="3"/>
<mx:TextInput id="ta2nd" text="3"/>
<mx:TextArea id="taMain"/>
<mx:Button id="b1" label="Compute Sum"
click="taMain.text=computeSum(Number(ta1st.text), Number(ta2nd.text))"/>
</mx:Application>
The source attribute of the <mx:Script> tag supports both relative and absolute paths. For more
information, see “Referring to external files” on page 51.
You cannot use the
source attribute and wrap ActionScript code inside the same <mx:Script>
tag. To include a file and write ActionScript in the MXML file, use two
<mx:Script> tags.