
Creating ActionScript components
53
Creating ActionScript components
You create reusable components using ActionScript, and reference these components in your Flex
applications as MXML tags. Components created in ActionScript can contain graphical elements,
define custom business logic, or extend existing Flex components. They can inherit from any
components available in Flex.
Defining your own components in ActionScript has several benefits. Components let you divide
your applications into individual modules that you can develop and maintain separately. By
implementing commonly used logic within custom components, you can build a suite of reusable
components that you can share among multiple Flex applications.
In addition, you can base your custom components on the set of Flex components by extending
from the Flex class hierarchy. You can create custom versions of Flex visual controls, as well as
custom versions on nonvisual components, such as data validators, formatters, and effects.
For example, you can define a custom button, derived from the Flex Button control, as the
following example shows:
class myControls.MyButton extends mx.controls.Button {
function MyButton() {
...
}
...
}
In this example, you write your MyButton control to the MyButton.as file, and you store the file
in the myControls subdirectory of the root directory of your Flex application. The fully qualified
class name of your component reflects its location. In this example, the component’s fully
qualified class name is
myControls.MyButton
.
You can reference your custom Button control from a Flex application file, such as MyApp.mxml,
as the following example shows:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:cmp="myControls.*"
>
<cmp:MyButton label="Jack" />
</mx:Application>
In this example, you first define the
cmp
namespace that defines the location of your custom
component in the application’s directory structure. You then reference the component as an
MXML tag using the namespace prefix.
Typically, you put custom ActionScript components in directories that are in the ActionScript
classpath. These include your application’s root directory, the
flex_app_root
/WEB-
INF/flex/user_classes directory, or any directory you specify in the
<actionscript-classpath>
tag in the flex-config.xml file. For more information, see Chapter 18, “Creating ActionScript
Components” in
Developing Flex Applications
.
Summary of Contents for FLEX-GETTING STARTED WITH FLEX
Page 1: ...Getting Started with Flex...
Page 4: ...4 Contents...
Page 22: ...22 Chapter 1 Introducing Flex...