20.8. Distinguishing Among Results from Multiple Calls to a Single Service

Problem

You want to be able to distinguish among results from multiple calls to a single service function.

Solution

Use instances of a custom response class for each response object.

Discussion

When a result is returned from a Flash Remoting service function, Flash doesn’t automatically tell you which service function invocation generated the result. Furthermore, due to the asynchronous nature of Flash Remoting, you cannot rely on results being returned to Flash in the same order in which the functions were invoked. However, you can distinguish between the results from calls to multiple service functions by using a separate instance of a custom class for each response object. Simply attach a custom property to each response object instance and check its value when the result from the service function is returned.

This solution uses a custom response class that takes an id parameter in the constructor. This allows you to create multiple instances of the class—one for each function invocation—and assign each one a unique id. Then, when the result comes back, you can distinguish between the results using the id property of the response object. For example:

// Define a constructor for a custom response object class that assigns an id 
// property to each instance.
function MyResponseObject(id) {
  this.id = id;
}

// Define the onResult(  ) method for the response object class. MyResponseObject.prototype.onResult = function ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.