3.5 Building Forms in Flash MX

HTML forms are structured elements, consisting of a <FORM> tag surrounding form elements. In Flash, there is no encompassing form tag. The form elements can be text fields, UI components, or other Flash objects and can reside anywhere in the movie. Because Flash is an object-oriented interface, you need only reference the objects by name to access their properties and methods. You can utilize the text property of a TextField object in your Flash Remoting service calls like this:

myService.myMethod(somefield_txt.text);

When you do that, you are essentially posting the contents of somefield_txt to the remote service. This is equivalent to sending a form and the contents of a form field to a server-side page. Because your remote services are expecting the argument, they don't need to process and parse a form field?they need only access the value.

For that reason, building forms in Flash is somewhat of a misnomer. You are not actually building a web form, per se; you are simply creating interface elements that act as collectors of user data and passing this data to your remote services. No form is ever created; no post is ever made to the server. This highlights a key difference between Flash Remoting and working with the LoadVars class (or working with loadVariables( ) in Flash 5).

When working with the LoadVars class, your server-side page has to parse the incoming form data and determine what to do with it. Working with Flash Remoting is almost like working in one environment: you simply pass arguments to remote methods and process the return value. The fact that the methods can be halfway around the world makes Flash Remoting powerful. Flash 2004 handles forms the same way as Flash MX. Flash Pro introduces an alternative to the timeline metaphor?variously called slides, screens, or forms?which should not be confused with submitting data from forms, as discussed here.

The UI components and other ActionScript objects each have their own properties, and some of them have methods to address these properties. Table 3-4 shows several objects that might be used in a Flash Remoting application and how their values can be grabbed through dot notation by accessing a method of the object (or accessing the text property directly in the case of a text field).

Table 3-4. Accessing properties of various ActionScript objects

Object

Get the data

AdvancedMessageBox

FAdvancedMessageBox.getButtons( )[buttonIndex]

Calendar

FCalendar.getSelectedItem( ) or FCalendar.getSelectedItems( )

Checkbox

FCheckBox.getValue( )

ComboBox

FComboxBox.getSelectedItem( )

DataGrid

FDataGrid.getSelectedItem( ) or FDataGrid.getItemAt(index)

IconButton

FIconButton.getLabel( ) or FIconButton.getValue( )

ListBox

FListbox.getSelectedItem( ), FListbox.getSelectedItem( ), FListbox.getSelectedIndex( ), or FListbox.getSelectedIndices( )

MessageBox

FMessageBox.getButton( )[buttonIndex]

PromptBox

FPromptBox.getUserInput( )

PushButton

FPushButton.getLabel( )

RadioButton

FRadioButton.getData( ) or FRadioButton.getValue( )

TextField

TextField.text

Tree

FTree.getSelectedNode( )

The AdvancedMessageBox and MessageBox components have a roundabout way of retrieving the user input. You must retrieve the index number of the button that was clicked and use it to read the label of the button from the Buttons array.

To do so, define a close callback handler, using setCloseHandler( ), that accepts two arguments: the component instance and the index of the clicked button. A typical callback function for a MessageBox or AdvancedMessageBox is shown in Example 3-2.

Example 3-2. Message Box demo
// Set up the MessageBox component named delete_mb
delete_mb.setButtons(["OK","Cancel"]);
delete_mb.setMessage("Are you sure?");
delete_mb.setTitle("Delete Record");
delete_mb.setCloseHandler("myCloseHandler");

myCloseHandler = function (myMessageBox_mb, buttonIndex) {
  // Get the label of the button that was pressed
  var buttonLabel = myMessageBox_mb.getButtons( )[buttonIndex];
  // Do something based on which button was pressed
  switch (buttonLabel.toLowerCase( )) {
    case "cancel":
      trace("cancel");
      // In practice, do nothing when cancelled
      return;
    case "ok":
      trace("ok");
      // In practice, call the remote service when user clicks OK, such as:
      // myRemoteService.deleteRecord(myRecordNumber);
      return;
  }
};


    Part III: Advanced Flash Remoting