Building a simple application

The following procedures describes how to build a simple server-side ActionScript application. The example application, a corporate personnel directory, uses the NetServices object to connect to the personneldirectory server-side ActionScript. The personneldirectory server-side ActionScript retrieves data from a ColdFusion data source and returns the results to the Flash movie as a RecordSet object.

Note:   The server-side ActionScript that you create provides the backend services in an application.

This example requires the following:

To create the application:

  1. Write the server-side ActionScript that performs the database query and returns data back to the client through the Flash Remoting service.
  2. Create the Flash movie interface.
  3. Define a search function that sends user data to the Flash service.
  4. Define a result function that captures the results returned from the Flash service.
  5. Ensure that the Flash movie has established a connection to the Flash Remoting service.

For more information, see the following sections:

Write the server-side ActionScript

In this example, you create a search function that performs a simple search operation against a ColdFusion data source. This function accepts two arguments, firstName and lastName, and returns any records found that match these parameters.

Create a server-side ActionScript file that contains the following code, and save the file as personneldirectory.asr:

//search takes firstName lastName arguments
function search(firstName, lastName)
{
  searchdata = CF.query({datasource: "bigDSN", 
    sql:"SELECT * from personnel WHERE fname = firstName AND lname = lastName"{);

  if (searchdata)
    return searchdata;
  else
    return null;
}

Creating the Flash movie interface

The Flash movie interface consists of one frame with a variety of text boxes and a submit button.

To create the Flash movie interface:

  1. In the Flash MX authoring environment, create a new Flash source file, and save it as pDirectory.fla.
  2. Create two input text boxes. Name one text box variable lastName and the other firstName.
  3. Create a dynamic text box, and name its variable status.
  4. Insert a list box component, and name it dataView.
  5. Insert a push button component.
  6. Save your work.

The following figure shows what the pDirectory Flash movie looks like:

This image shows what the Flash MX movie should look like for the current example

Submitting user data to the Flash Remoting service

To send data to a server-side ActionScript, you must create a function that passes the data from the Flash movie to the server-side ActionScript. The search function, applied at the frame level, collects the user-entered data from the firstName and lastName text boxes and passes the data as function arguments to the directoryService object, which is created when the Flash movie connects to the Flash Remoting service. For more information, see "Checking for a Flash Remoting service connection".

The following is a Flash MX ActionScript example:

#include "NetServices.as"
function search() 
{
  // The search() method is defined in the server-side AS file
  directoryService.search(firstName.text, lastName.text);
  dataView.setDataProvider(null);
  status.text = "waiting...";
}

In this example, the search function passes the contents of firstName and lastName text boxes to the server-side ActionScript.

The dataView.setDataProvider(null) function clears the dataView list box component. The status.text action displays a message in the status text box while the record set is being retrieved from the server-side ActionScript.

Capturing Flash Remoting service results

When you create a function that calls a server-side ActionScript function, you must also create a function to handle the data returned by the server-side ActionScript. You access data returned by a server-side ActionScript by defining a function with the same name as the function making the initial call, only you append _Result to the name.

For example, if you create a function called basicQuery to return query data, you also need to define a results function to handle returned data; the results function would be declared as basicQuery_result.

In the following example, the results function search_Result supplies the record set to the dataView.setDataProvider function:

function search_Result(resultset) 
{
  dataView.setDataProvider(resultset);
  status.text = (0+resultset.getLength())+" names found.";
}

In this example, the _Result suffix tells the Flash Remoting service to return the results of the search function to this function. The dataView.setDataProvider(resultset) function assigns the results returned by the Flash Remoting service to the dataView list box. The status.text action displays the number of records returned by the Flash Remoting service.

Checking for a Flash Remoting service connection

To ensure that the Flash movie is connected to the Flash Remoting service, you use an if statement; for example:

if (inited == null) 
{
  inited = true;
  NetServices.setDefaultGatewayUrl("http://localhost:8100/flashservices/
    gateway");
  gateway_conn = NetServices.createGatewayConnection();
  directoryService = gateway_conn.getService(personneldirectory, this);
  status.text = "Type into the text boxes, then click 'Search'";
}

In this example, inited is evaluated for a value. If it is null (not connected), the movie connects to the Flash Remoting service using the NetServices object. For more information about connecting to the Flash Remoting service, see "Connecting to the Flash Remoting service".

Comments