ActionScript in Flex Applications - Using ActionScript (Page 2 of 4 )
When you want to use ActionScript within Flex, you have four basic options for where to place the code:
Inline within MXML tags
Nested within MXML tags
In MXML scripts
Within ActionScript classes
The preceding lists the techniques for working with ActionScript code, from the simplest to the most complex form. We’ll look at each of these techniques in the following sections.
In this example, the text assigned to theclickevent handler attribute is ActionScript code, which calls ashow()method of an ActionScript class calledAlert.
This example uses the ActionScript expressioninput.textto evaluate thetextproperty value for theinputobject (the text input control).
Inline data binding represents the most limited use of ActionScript, because it can evaluate only one expression. For instance, the preceding example evaluates the expressioninput.text. You could use a more complex expression, such as the following:
This example concatenates the stringUser input:with the user input from the text input control. You can also create even more complex expressions using inline data binding.
Inline event handlers allow you to write more complex ActionScript that can consist of several statements. ActionScript statements generally end with semicolons. The following example illustrates a button with slightly more complex event handler code, consisting of two expressions:
This example first displays an alert dialog box. It then moves the button to the right by 40 pixels. Although you can string together many statements (as in this example), it is very uncommon. It’s not difficult to understand why this would be. Rather simply: the code is difficult to read and manage when you try to use several inline statements in that fashion. If an event handler needs to run several statements, it is far more common to simply call a function. We’ll look more at functions in the next section, and then later in the chapter, in the “Methods ” section.