MACROMEDIA FLEX-GETTING STARTED WITH FLEX Getting Started page 48

Table of Contents

Advertisement

The script within a given
The
<mx:Script>
tag or other top-level component tag). You can define multiple script blocks in your MXML files,
but you should try to keep them in one location to improve readability.
The following example declares a variable and a function:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script>
<![CDATA[
var z:Number;
function doSomething() {
z = z + 1;
}
]]>
</mx:Script>
...
</mx:Application>
Including ActionScript code versus importing ActionScript classes
To make your MXML code more readable, you can also reference ActionScript files in your
tags, rather than insert large blocks of script. You can include or import
<mx:Script>
ActionScript files.
There is a distinct difference between including and importing in ActionScript. Including is
copying lines of code from one file into another. Importing is adding 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 ActionScript classpath. Files that you include must be located
relative to the application root or use an absolute path.
You use the
#include
ActionScript code snippets to your Flex applications.
You use
import
your Flex applications.
Including ActionScript files
Flex lets you point to external ActionScript files in your
compiler copies the entire contents of the file into your MXML application. As with ActionScript
in an
<mx:Script>
functions. 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,
since the MXML file itself is a class.
48
Chapter 3: Using ActionScript
<mx:Script>
tag must be located at the top level of the MXML file (within the Application
// This must be in a function.
directive or the
statements in an
<mx:Script>
block, ActionScript in included files must be variable declarations or in
tag is accessible from any component in the MXML file.
<mx:Script source="filename">
block to add ActionScript classes and packages to
<mx:Script>
tag to add
tags. At compile-time, the

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLEX-GETTING STARTED WITH FLEX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flex

Table of Contents