
48
Chapter 3: Using ActionScript
The script within a given
<mx:Script>
tag is accessible from any component in the MXML file.
The
<mx:Script>
tag must be located at the top level of the MXML file (within the Application
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; // This must be in a function.
}
]]>
</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
<mx:Script>
tags, rather than insert large blocks of script. You can include or import
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
directive or the
<mx:Script source="filename">
tag to add
ActionScript code snippets to your Flex applications.
You use
import
statements in an
<mx:Script>
block to add ActionScript classes and packages to
your Flex applications.
Including ActionScript files
Flex lets you point to external ActionScript files in your
<mx:Script>
tags. At compile-time, the
compiler copies the entire contents of the file into your MXML application. As with ActionScript
in an
<mx:Script>
block, ActionScript in included files must be variable declarations or in
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.
Содержание FLEX-GETTING STARTED WITH FLEX
Страница 1: ...Getting Started with Flex...
Страница 4: ...4 Contents...
Страница 22: ...22 Chapter 1 Introducing Flex...