When the service function results return from the application server, event handlers use the returned data or handle the returned error information. For example, a result handler could display the results in the Flash application, and an error handler could set trace functions to report the error descriptions. This section describes how to handle result data. The section "Handling errors" describes how to handle error information.
Flash Remoting MX supports the following hierarchy of event handling:
NetConnection object getService method, Flash Remoting MX does the following:onResult, Flash Remoting MX returns the result for the function to that function.onResult method.Note: Do not specify a responder object in the gatewayConnection.getService method and in the service function call. If you specify the responder in both places, Flash Remoting MX passes the responder you specify in the service function method to the service function as an argument. This behavior causes errors in your application.
The different types of result handlers provide you with a variety of result-handling strategies you can use to match your application's needs. The following sections describe some of the techniques you can use:
If you use the gatewayConnection.getService method to specify the responder object, you can use the following techniques:
this object, as the responder in the gatewayConnection.getService method and create a separate _Result handler on the main movie timeline for each service function. Use this technique if all of the following conditions are true:
this object as the responder in the gatewayConnection.getService method and create an onResult method on the main movie timeline to handle some or all of the service function results. Use this technique if the results of all requests for all service functions can be handled in the same manner.this object as the responder in the gatewayConnection.getService method, and create a separate _Result handler for some service functions. Create a single onResult result handler for the remaining service functions. Use this technique if the following are true:For an example of this technique, see "Example: using a hierarchy of handlers" .
The following example shows how you can use the main movie as the responder and have a common onResult result handler for some service functions, and function-specific functionName_Result handlers for other functions. In this example, there are two function-specific result handlers, getTemperature_Result and getForecast_Result, which display the returned temperature and forecast in specific text boxes. The default onResult response handler displays the result in a general-purpose message box.
// Initialization code, run once for each movie instance.
if (inited == null)
{
inited = true;
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway")
gatewayConnection = NetServices.createGatewayConnection();
// Specify the main movie (this) as the default responder object.
weatherService = gatewayConnection.getService("flashExamples.weatherStation",
this);
}
// Function-specific result handlers
function getTemperature_Result(temperature)
{ temperatureIndicator.text = temperature; }
function getForecast_Result(forecast)
{ forecastIndicator.text = forecast; }
// Default response handler
function onResult(result)
{ generalMessageBox.text = result; }
// Call the service functions
weatherService.getTemperature("New York");
weatherService.getForecast("Chicago");
weatherService.getServiceStatus("San Francisco");
weatherService.getUsageStats();
If you specify the responder when you call the service function methods, you can use the following techniques:
.getService method.
This technique is more object-oriented than using the this object as a responder, and lets you program using more encapsulated code than the techniques in "Using the getService method to specify a responder".
This technique lets you create function-specific responders where several services have the same function names but return different data. It also lets you create several different handlers that you can use, under different circumstances, with a single service function.
For an example of this technique, see "Example: specifying unique result objects in service function calls" , next.
The following example rewrites the example in the "Example: using a hierarchy of handlers" to have the service function calls specify result handlers. There are three response-handler objects: tempResult for the temperature, forecastResult for the forecast, and generalResult for other service functions. Each result handler has one function, onResult, to handle the result returned by the service.
// Initialization code, run once for each movie instance.
if (inited == null)
{
inited = true;
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway")
gatewayConnection = NetServices.createGatewayConnection();
// Do not specify a default responder object when creating the service object.
weatherService = gatewayConnection.getService("flashExamples.weatherStation");
}
// Temperature result handler object
function tempResult()
{
this.onResult = function (temperature)
{ temperatureIndicator.text = temperature; }
}
//Forecast result handler object
function forecastResult()
{
this.onResult = function (forecast)
{ forecastIndicator.text = forecast; }
}
// General result handler object
function generalResult ()
{
this.onResult = function (result)
{ generalMessageBox.text = result; }
}
// Call the service functions and specify the result handler as the first argument.
weatherService.getTemperature(new tempResult(), "New York");
weatherService.getForecast(new forecastResult(), "Chicago");
weatherService.getServiceStatus(new generalResult(), "San Francisco");
weatherService.getUsageStats(new generalResult());
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/UseActionScript7.htm
Comments
No screen name said on Jan 5, 2004 at 11:42 AM : Adrian Cadena said on Apr 16, 2004 at 12:39 PM : No screen name said on Jun 17, 2004 at 6:42 PM : Mark Kerecz said on Jul 5, 2004 at 1:46 PM :