There's been some talk lately on FlexCoders about Cairngorm's Responder interface and how to use it to handle results from a remote service. Personally, I don't use Cairngorm's Responder. Instead, I use the mx.rpc.IRsponder interface.
The differences are subtle, but I prefer IResponder for the following reasons:
- IResponder ships with the Flex 2 framework
- IResponder integrates well with other classes in the mx.rpc package, specifically AsyncToken
- IResponder allows multiple responders to act on the return value of a remote service call
- Cairngorm's Responder requires some "hard-wiring" and isn't as type-safe (which makes it more error prone)
To understand the last two points, some background on Cairngorm's Responder is needed. Cairngorm's Responder interface has two public methods:
public function onResult(event:* = null):void
public function onFault(event:* = null):void
Typically, commands that you create inside of the Cairngorm framework will implement this interface. Commands generally make a remote method call and handle the results of that call all in one place. Every command that makes a remote method call will adhere to the Responder interface and define an onResult and onFault method.
In order to use the Responder interface though, you need a little bit of glue-code behind the scenes. When you define the remote service you have to explicitly add a result and a fault listener in the tag, like this:
// In Services.mxml
<mx:RemoteObject id="someService" result="event.token.resultHandler( event )" fault="event.token.faultHandler( event )" ... />
Here you're saying that every result should invoke the resultHandler method and every fault should invoke the faultHandler (passing along the actual event object with it).
The call to the remote service normally happens in a business delegate. An example business delegate class looks something like this: