Recipe 20.7 Handling Flash Remoting Results

20.7.1 Problem

You want to handle the results from calling a service function using Flash Remoting.

20.7.2 Solution

Use a named result function or, as a better alternative, use a response object.

20.7.3 Discussion

Unlike invocations of functions and methods that are defined within the Flash movie, Flash Remoting service function invocations are asynchronous. This means that when the ActionScript interpreter encounters a remote service function call within the code, it initiates the call but does not wait for a response before continuing to the next line of ActionScript code. The same thing is true when loading any external content, be it by means of an XML object, a LoadVars object, or a movie clip using loadMovie( ). This asynchronous behavior allows the Flash movie to continue to run without waiting on something that has the potential for high latency. Therefore, if a remote service function returns a value, you cannot use that value within your ActionScript code immediately, as you would the return value of a native ActionScript function or method.

// myVariable is undefined because the service function 
// does not return a value immediately.
myVariable = myService.myServiceFunction(responseObject);

Instead, you have to use a response object with callback functions or methods to obtain the result. There are two ways to handle results from a Flash Remoting service function, and they correspond to the two variations of invoking the service function (see Recipe 20.6).

The first technique, named result functions, is used, by convention, when you have specified a default response object for a service object (that is, when you've passed a response object to the getService( ) method). Named result functions are functions that must be named with the pattern serviceFunctionName_Result( ). For example, if the service function is named getUserList( ), then the named result function should be named getUserList_Result( ). The Flash Remoting gateway automatically passes any returned value to the result function as a parameter. For example:

// Define the named result function getUserList_Result(  ) to handle the result from
// the service function getUserList(  ).
function getUserList_Result (result) {

// Create a service object from a connection object. Define the default response
// object as this, since this is where the result function is defined.
myService = myConnection.getService("serviceName", this);

// Invoke getUserList(  ).
myService.getUserList(  );

The second technique is to use a response object that declares an onResult( ) method. By convention, you should use this technique when you are defining a response object for each service function invocation. This technique is much more flexible than the first, in that you can define different response objects for each service function invocation, or you can share a single response object among multiple service function invocations. Note that this, which represents the current object, is passed as the response object in the preceding example.

To create a response object, define an onResult( ) method on it. The onResult( ) method is automatically invoked when a result is returned to the Flash movie from the service function, and any returned value is passed to the onResult( ) method as a parameter. For example:

// Create an object and define an onResult(  ) method for it.
myResponseObject = new Object(  );
myResponseObject.onResult = function (result) {

// Create the service object from a connection object. Do not set 
// a default response object.
myService = myConnection.getService("serviceName");

// Call the getUserList(  ) service function and specify that the response should be
// handled by the myResponseObject response object.

20.7.4 See Also

Recipe 20.6 and Recipe 20.8

    Part I: Local Recipes
    Part II: Remote Recipes