Adobe Flex 3 Help

Comparing, including, and importing ActionScript code

To make your MXML code more readable, you can reference ActionScript files in your <mx:Script> tags, rather than insert large blocks of script. You can either include or import ActionScript files.

There is a distinct difference between including and importing code in ActionScript. Including copies lines of code from one file into another, as if they had been pasted at the position of the include statement. Importing adds a reference to a class file or package so that you can access objects and properties defined by external classes. Files that you import must be found in the source path. Files that you include must be located relative to the file using the import statement, or you must use an absolute path.

You use the include statement or the <mx:Script source="filename"> tag to add ActionScript code to your Flex applications.

You use import statements in an <mx:Script> block to define the locations of ActionScript classes and packages that your Flex applications might use.

Including ActionScript files

To include ActionScript code, you reference an external ActionScript file in your <mx:Script> tags. At compile time, the compiler copies the entire contents of the file into your MXML application, as if you had actually typed it. As with ActionScript in an <mx:Script> block, ActionScript statements can only be inside functions. Included files can also declare constants and namespaces, include other ActionScript files, import declarations, and use namespaces. You cannot define classes in included files.

Variables and functions defined in an included ActionScript file are available to any component in the MXML file. An included ActionScript file is not the same as an imported ActionScript class. Flex provides access to the included file's variables and functions, but does not add a new class, because the MXML file itself is a class.

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.

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 statement inside <mx:Script> blocks.

Using the source attribute to include ActionScript files

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.

Do not give the script file the same name as the application file. This causes a compiler error.

The following example shows the contents of the IncludedFile.as file:

// usingas/includes/IncludedFile.as
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 includes subdirectory.

<?xml version="1.0"?>
<!-- usingas/SourceInclude.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script source="includes/IncludedFile.as"/>

    <mx:TextInput id="ta1st" text="3" width="40" x="170" y="24" textAlign="right"/>
    <mx:TextInput id="ta2nd" text="3" width="40" x="170" y="52" textAlign="right"/>

    <mx:TextArea id="taMain" height="25" width="78" x="132" y="82" textAlign="right"/>

    <mx:Button id="b1" label="Compute Sum" 
        click="taMain.text=String(computeSum(Number(ta1st.text), Number(ta2nd.text)));" 
        x="105" 
        y="115"
    />

    <mx:Label x="148" y="52" text="+" fontWeight="bold" fontSize="17" width="23"/>
</mx:Application>

The executing SWF file for the previous example is shown below:

The source attribute of the <mx:Script> tag supports both relative and absolute paths. For more information, see Referring to external files that have been included.

You cannot use the source attribute of an <mx:Script> tag and wrap ActionScript code inside that same <mx:Script> tag. To include a file and write ActionScript in the MXML file, use two <mx:Script> tags.

Using the include directive

The include directive is an ActionScript statement that copies the contents of the specified file into your MXML file. The include directive uses the following syntax:

include "file_name";

The following example includes the myfunctions.as file:

<?xml version="1.0"?>
<!-- usingas/IncludeASFile.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script><![CDATA[

        /*  The myfunctions.as file defines two methods that 
            return Strings. */
        include "includes/myfunctions.as";

    ]]></mx:Script>

    <mx:Button id="myButton" 
        label="Call Methods in Included File" 
        click="ta1.text=doSomething();ta1.text+=doSomethingElse()"
    />
    <mx:TextArea width="268" id="ta1"/>
    <mx:Button label="Clear" click="ta1.text=''"/>

</mx:Application>

The executing SWF file for the previous example is shown below:

You can specify only a single file for each include directive, but you can use any number of include directives. You can nest include directives; files with include directives can include files that have include directives.

The include directive supports only relative paths. For more information, see Referring to external files that have been included.

You can use the include only where multiple statements are allowed. For example, the following is not allowed:

if (expr)
    include "foo.as"; // First statement is guarded by IF, but rest are not.
...

The following is allowed:

if (expr) {
    include "foo.as"; // All statements inside { } are guarded by IF.
}

The use of curly braces ({ }) allows multiple statements because you can add multiple statements inside the braces.

Adobe recommends that you not use the include directive if you use a large number of included ActionScript files. You should try to break the code into separate class files where appropriate and store them in logical package structures.

Referring to external files that have been included

The source attribute of the <mx:Script> tag and the include directive refer to files in different ways.

The following are the valid paths to external files that are referenced in an <mx:Script> tag's source attribute:

  • Relative URLs, such as ../myscript.as. A relative URL that does not start with a slash is resolved relative to the file that uses it. If the tag <mx:Script source="../IncludedFile.as"> is included in "mysite/myfiles/myapp.mxml," the system searches for "mysite/IncludedFile.as".

For an ActionScript include directive, you can reference only relative URLs. Flex searches the source path for imported classes and packages. Flex does not search the source path for files that are included using the include directive or the source attribute of the <mx:Script> tag.