Building a simple application



The following procedure 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 SWF file as a RecordSet object.

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

This example requires the following:

  • A server-side ActionScript file named personneldirectory.asr that includes functions that interact with a ColdFusion data source.

  • A client-side SWF movie in which the NetServices object is created.

Create the application

  1. Write server-side ActionScript that performs the database query and returns data to the client through the Flash Remoting service.

  2. Create the SWF movie interface. See Creating the SWF movie interface.

  3. Define a search function that sends user data to the Flash Remoting service. See Submitting user data to the Flash Remoting service.

  4. Define a result function that captures the results returned from the Flash Remoting service. See Capturing Flash Remoting service results.

  5. Ensure that the SWF movie has established a connection to the Flash Remoting service. See Checking for a Flash Remoting service connection.

Writing the server-side ActionScript function

The example here creates 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 arguments.

Create a server-side ActionScript function

 Create a server-side ActionScript file that contains the following code:
//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 SWF movie interface

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

  1. In the Flash authoring environment, create a 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.

Submitting user data to the Flash Remoting service

To send data to server-side ActionScript, create a function that passes the data from the SWF movie to 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 SWF movie connects to the Flash Remoting service. For more information, see Checking for a Flash Remoting service connection.

The following is a Flash 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..."; 
}

Reviewing the code

The following table describes the code and its function:

Code

Description

directoryService.search

(firstName.text, lastName.text);

Passes the contents of the firstName and lastName text boxes to server-side ActionScript.

dataView.setDataProvider

(null);

Clears the dataView list box component.

status.text = "waiting...";

Displays a message in the status text box while the record set is being retrieved from server-side ActionScript.

Capturing Flash Remoting service results

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

For example, if you create a function called basicQuery to return query data, you define a results function to handle returned data; declare the results function 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."; 
}

Reviewing the code

The following table describes the code and its function:

Code

Description

function search_Result

(resultset)

The _Result suffix tells the Flash Remoting service to return the results of the search function to this function.

dataView.setDataProvider

(resultset);

Assigns the results returned by the Flash Remoting service to the dataView list box.

status.text = (0+resultset.

getLength())+" names found.";

Displays the number of records returned by the Flash Remoting service.

Checking for a Flash Remoting service connection

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

if (inited == null)  
{ 
    inited = true; 
    NetServices.setDefaultGatewayUrl("http://localhost:8500/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, the inited variable is evaluated for a value. If inited 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.