Invoking Ruby objects with <mx:RemoteObject>
Search:
 

MINI TUTORIAL

Invocation of a Ruby object using the <mx:RemoteObject> MXML declaration can be done in just a few simple steps. This mini tutorial will guide through the setup and configuration:
  1. Download and install WebORB for Rails (use the Getting Started guide if you need assistance with this step).
  2. Deploy the Ruby class you will be invoking from Flex into /app/services folder of your Rails application.
  3. Open /config/WEB-INF/flex/remoting-config.xml in a text editor and add a destination declaring your Ruby class. Use the following format:
  4. <destination id="destination-name">
       <properties>
         <source>Your/Ruby/ClassName</source>
       </properties>
    </destination>
    
    where,
    • destination-name is an id assigned to your Ruby class. The id will be used by Flex client when invoking server-side methods
    • Your/Ruby/ClassName is the full name of the Ruby class exposed as a service to Flex clients
       
  5. Create a Flex Builder project as it is described in the "GETTING STARTED - CREATING FLEX APPLICATION" section in the this article.
  6. Add the following remote object declaration to your MXML's application markup:
    <mx:RemoteObject id="remote-object-id" destination="destination-name"
                     showbusycursor="true" fault="faultHandler(event)">
        <mx:method name="methodToInvoke" result="successHandler(event)">
    </mx:RemoteObject>
    
    where,
    • destination-name must be the same literal as destination id from remoting-config.xml
    • remote-object-id is an id used in the MXML application to refer to a remote object
    • methodToInvoke is the method name available in the remote destination that your remote object can invoke
    • successHandler and faultHandler are function references your MXML application will invoke upon successful or unsuccessful method invocation
       
  7. Suppose the following Ruby class needs to be consumed by a Flex client (Note: WebORB will be able to automatically load this class if it is placed within a file named MyRubyHelloWorld.rb and dropped into the /app/services directory).
    class MyRubyHelloWorld
       def sayWorld( string )
         "Hey Flex"
       end
    end
    
    then the destination declaration may look as the following:
    <destination id="helloWorldDestination">
      <properties>
         <source>MyRubyClassHelloWorld</source>
      </properties>
    </destination>
    The <RemoteObject> declaration would have the following contents:
    <mx:RemoteObject id="helloWorldService" destination="helloWorldDestination"
                     showbusycursor="true" fault="faultHandler(event)">
        <mx:method name="sayHello"result="gotHello(event)">
    </mx:RemoteObject>
    The callback handler functions (gotHello and faultHandler) could be declared as:

    private
    function gotHello(event:ResultEvent):void
    {
       Alert.show("Server said: " + event.result, "Success");
    }

    private function faultHandler(event:FaultEvent):void
    {
       Alert.show(event.fault.faultstring, "Error");
    }

     
  8. Now anywhere in your MXML application you can invoke sayHello using the following line of code: helloWorldService.sayHello();
 
Copyright © 2003-2006 Midnight Coders, LLC.  Privacy Policy | Contact Webmaster
Home | Products | Customers | Download | Licensing | Forum | Developers | About