Defining multiple RemoteObjects in MXML

Is it possible to define multiple RemoteObjects in MXML?

For instance:

//amf_tests.mxml


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="absolute"
 creationComplete="init()">

<mx:Script>
<![CDATA[

  import mx.collections.ArrayCollection;
  import mx.rpc.events.ResultEvent;
  import mx.controls.Alert;

  [Bindable] private var serviceTypes:Object; // the remote object service names and instances
  [Bindable] private var ro:RemoteObject; // the selected service

  private function init() {
    serviceTypes = { amfphp: amfphp_ro, zendamf: zendamf_ro, sabreamf: sabreamf_ro };
  }

  private function sendRemoteServiceRequest():void {
    ro = serviceTypes[serviceType.text];

    ro.sayHi();
    ro.sayBye();
  }

  private function resultHandler(event:ResultEvent):void {
    Alert.show(event.result.toString());
  }

  private function faultHandler(event:FaultEvent):void {
    Alert.show(event.fault.faultString);   
  }

  private function getKeys(obj:Object):ArrayCollection {
    var keys:ArrayCollection = new ArrayCollection();

    for (var key:String in obj) {
      keys.addItem(key);
    }

    // there's got to be a better way to sort than this
    keys = new ArrayCollection(keys.toArray().sort(Array.CASEINSENSITIVE));
    return keys;
  }

\]]>
</mx:Script>

<mx:RemoteObject id="amfphp_ro" source="amf_tests_amfphp.HelloWorld" destination="amfphp">  
  <mx:method name="sayHi" result="resultHandler(event)"/>   
  <mx:method name="sayBye" result="resultHandler(event)"/> 
</mx:RemoteObject>

  <mx:RemoteObject id="zendamf_ro" source="amf_tests_zendamf.HelloWorld" destination="zenfamf">  
    <mx:method name="sayHi" result="resultHandler(event)"/>   
    <mx:method name="sayBye" result="resultHandler(event)"/>
  </mx:RemoteObject>

  <mx:RemoteObject id="sabreamf_ro" source="amf_tests_sabreamf.HelloWorld" destination="sabreamf">
    <mx:method name="sayHi" result="resultHandler(event)" fault="faultHandler(event)"/> 
    <mx:method name="sayBye" result="resultHandler(event)"  fault="faultHandler(event)"/>
  </mx:RemoteObject>

  <mx:HBox>     <mx:Label text="Service Type:"/>
    <mx:ComboBox id="serviceType" dataProvider="{getKeys(serviceTypes)}"/> 
    <mx:Button label="Send Request" click="sendRemoteServiceRequest()"/>
  </mx:HBox>

</mx:Application>

// services-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<services-config>

 <services>

 <service id="amfphp-flashremoting-service"
 class="flex.messaging.services.RemotingService"
 messageTypes="flex.messaging.messages.RemotingMessage">
 <destination id="amfphp">
 <channels>
 <channel ref="my-amfphp"/>
 </channels>
 <properties>
 <source>*</source>
 </properties>
 </destination>
 </service>

 <service id="zendamf-flashremoting-service"
 class="flex.messaging.services.RemotingService"
 messageTypes="flex.messaging.messages.RemotingMessage">
 <destination id="zendamf">
 <channels>
 <channel ref="my-zendamf"/>
 </channels>
 <properties>
 <source>*</source>
 </properties>
 </destination>
 </service>

 <service id="sabreamf-flashremoting-service"
 class="flex.messaging.services.RemotingService"
 messageTypes="flex.messaging.messages.RemotingMessage">>
 <destination id="sabreamf">
 <channels>
 <channel ref="my-sabreamf"/>
 </channels>
 <properties>
 <source>*</source>
 </properties>
 </destination>
 </service>
   </services>

 <channels>

 <channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
 <endpoint uri="/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
 </channel-definition>

 <channel-definition id="my-zendamf" class="mx.messaging.channels.AMFChannel">
 <endpoint uri="/zendamf/index.php" class="flex.messaging.endpoints.AMFEndpoint"/>
 </channel-definition>

 <channel-definition id="my-sabreamf" class="mx.messaging.channels.AMFChannel">
 <endpoint uri="/sabreamf/index.php" class="flex.messaging.endpoints.AMFEndpoint"/>
 </channel-definition>

 </channels>

</services-config>

Maybe there’s a typo, but amfphp works, and all the others claim there’s no channel associated with the destination:

[MessagingError message=’Destination ‘sabreamf’ either does not exist or the destination has no channels defined (and the application does not define any default channels.)’]

My workaround is to declare the other RemoteObjects in the init() function and not try to use the <mx:RemoteObjects> and the services-config.xml. Definitely less elegant:

 
private function init():void
 {
   zendamf_ro.source = "HelloWorld";
   zendamf_ro.destination = "zendamf";
   zendamf_ro.endpoint = "http://localhost/zend_amf/index.php";
   sabreamf_ro.source = "HelloWorld";
   sabreamf_ro.destination = "sabreamf";
   sabreamf_ro.endpoint = "http://localhost/SabreAMF/index.php";

   serviceTypes = {amfphp: amfphp_ro, zendamf: zendamf_ro, sabreamf: sabreamf_ro};
  }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s