Flex and Remote Objects: Using the Same Remote Method For Different Purposes
There are a lot of really great tutorials out on Drupal.org and Adobe's sites which describe methods for using Flex with Drupal through Services and AMFPHP. (For a list of some tutorials, I'll post references at the bottom of the post) Remoting in Flex is super easy with the <mx:RemoteObject> but the one thing I noticed with all of the tutorials is that they all describe just one way of using RemoteObjects and Methods to get data from Drupal.
Imagine, if you will, that you have built a super awesome Drupal site heavily using the Views module. Now you want to tap into all of that data to create a Flex application, whether it's a new Druplex front-end for your entire site or maybe just a simple management tool. Following the examples at Adobe and on Drupal.org you set your site up your remote object for Views and add the only method defined in the Views service: getView().
Now, at this point, if you continue to follow the previous examples, you will set up result and fault handlers here in your mx:method tag, like this:
<mx:RemoteObject showBusyCursor="true" destination="amfphp" source="views" id="views"> <mx:method name="getView" result="onViewsResult(event)" fault="onFault(event)" /> </mx:RemoteObject>
The problem, though, is that now if you want to use getView to load three different views that do different things, you have to write all of that logic into a giant switch statement in your single result handler (onViewsResult).
Instead, if you declare your RemoteObject and then use an AsyncToken you can pass each individual call of a service method its own result handlers. Let me show you how:
In your MXML declare your RemoteObject like this:
<mx:RemoteObject showBusyCursor="true" destination="amfphp" source="views" id="views"> <mx:method name="getView"/> </mx:RemoteObject>
Note the lack of result or fault declarations in the method.
Then, in your ActionScript, when you make your remote services call do it like so:
private function setUpNews():void { // load the news and then inject into the vbox var fields:Array = ['title','teaser','url']; var news:AsyncToken = views.getView(apiKey,'news_feed',fields); news.addResponder(new mx.rpc.Responder(onNewsResult, onNewsFault)); } private function onNewsResult(e:ResultEvent):void { // result handler loop here } private function onNewsFault(e:FaultEvent):void { // error handling }
By naming an AsyncToken for the specific instance of views.getView() that you are calling, you are able to add custom Responders.
Sidenote: In Flex, there are two Responder classes built in (mx.rpc.Responder and flash.net.Responder) so you'll need to declare the mx.rpc.Responder explicitly.
Now you can split your result handling out into separate functions which are meant to deal only with the data coming back to you as a result of a particular Services call.
Resources:
http://www.adobe.com/devnet/flex/articles/drupal.html / http://groups.drupal.org/node%252F2768
http://drupal.org/node/118136
http://drupal.org/node/184529
http://drupal.org/node/187998
http://drupal.org/node/183123
http://drupal.org/node/178085
