Responders

Using a Flex remote service requires that the developper provide a Responder for the call.

Usually, this is done by the kind following code, in a PureMVC Proxy :

/* Creation of an object representing the call */
var call : AsyncToken = service.getReceivedNotifications();

/* Adding the handler for the success or the failure of the Call */
call.addResponder(new Responder(function (data : Object) : void {

    /* We get the result, and send it wrapped in a Notification */
    var result : ResultEvent = data as ResultEvent;
    sendNotification("received_notifications_fetched", result.result);

}, function( info : Object ) : void {

    /* In case of a failure, we also send it wrapped to another Notificat */
    var fault : Fault = info.fault as Fault;
    sendNotification("fault", fault);
}));

The SendNotificationResponder is a specialized type of IResponder that send PureMVC Notification. You only need to specify the notification for the success and for the failure of the call.

With this IResponder, the code before become :

/* Creation of an object representing the call */
var call : AsyncToken = service.getReceivedNotifications();

/* Adding the handler for the success or the failure of the Call */
call.addResponder(new SendNotificationResponder("received_notifications_fetched", "fault"));

The code length is reduced and then more readeable. This could help to reduces the error.

One step further

Sometimes, the result of the call, in case of success, has to be store in a Proxy. The SendNotificationResponder can store the result in a Proxy that is specified at the construction.

The SendNotificationResponder will store the result using the setData() method.