Friday, May 16, 2008

Adobe Flex: Specifying Remote Object End Point

Update September 2009:  (In Adobe Flex SDK 3.3 and older, there is a difference between mx.rpc.remoting.RemoteObject and mx.rpc.remoting.mxml.RemoteObject.  The first one does not have an endpoint parameter that is why you can't dynamically specify the endpoint using Actionscript.

That is why I use mx.rpc.remoting.mxml.RemoteObject.

But as of Adobe Flex SDK 3.4, you can now use endpoint parameter with mx.rpc.remoting.RemoteObject. )


This is the sample code I use to call a Remote Object in Actionscript rather than the usual Adobe Flex MXML tag.

There are some advantages to doing your code this way. Like you can specify the endpoint dynamically.

You will need this import statements.


import mx.events.CloseEvent;
import mx.collections.ArrayCollection;
import mx.utils.ArrayUtil;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.remoting.mxml.RemoteObject;

var clientrecs:ArrayCollection;
...


I use a one fault handler for all remote object calls in case a problem occurs.

private function faultHandler (event:FaultEvent):void {

      Alert.show(event.fault.faultString, 'Error');
} // faultHandler

This is a sample list function. In this case I am using AMFPHP. And of course my server side business objects are written in PHP.

private function listClients():void {

      var clientRO:RemoteObject = new RemoteObject();
     clientRO.endpoint = "amfphp/gateway.php";
     clientRO.source = "ClientObj" // PHP Object Name
    clientRO.destination = "amfphp";
    clientRO.listClients.addEventListener("result", listResult); // resutl handler
    clientRO.addEventListener("fault", faultHandler); // fault handler
    clientRO.listClients(cmbClientType.text); // call the service

}

A thing to remember. Your Remote Object source must be exactly the same as you have written in the Actionscript code. In this case its ClientObj. Remember this is case sensitive.

// Result Handlers
private function listResult(event:ResultEvent):void {

     clientrecs = new ArrayCollection(ArrayUtil.toArray(event.result));


}

Finally I pass the result to an Array Collection variable which I then bind to a DataGrid.

Goodluck.