This post is to carry on from my previous post regarding moving data from ColdFusion CFCs to Flex 2 Applications Using Webservices.

Here is the first sample showing basic retrieval of a string via Remote Object:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" >
    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
           
            [Bindable]
            public var sResult:String;
           
            public function handleStringResult(event:ResultEvent):void{
                sResult=event.result as String
            }
         
        ]]>
    </mx:Script>
       
    <mx:RemoteObject
        id="myService"
        destination="ColdFusion"
        source="services.HelloWorld">  
        <mx:method name="sayHelloString" result="handleStringResult(event)"  fault="Alert.show(event.fault.message)" />
    </mx:RemoteObject>

    <mx:Label id="lblStringResult" text="{sResult}"/>   
    <mx:Button label="get String Remote Object" click="myService.sayHelloString()"/>

</mx:Application>
 

This movie requires Flash Player 9

Browse the source of this example.
Download a zipfile containing the source to this sample.

The key to getting this to work revolves around 3 things:
1. Getting your config files located and configured properly for compilation.
2. Getting the fully qualified name for your cfc correct.
3. Knowing the endpoint URI for your Flex Gateway fro your Coldfusion server.

Getting this right is very exciting and I figured it would make a good blog entry on its own, so I have posted it here.

Here is the code and sample that demonstrates retrieving more datatypes from coldfusion via RemoteObject:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" >
    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
            import mx.collections.ArrayCollection;
           
            [Bindable]
            public var sResult:String;
           
            [Bindable]
            public var aResult:Array;
           
            [Bindable]
            public var oResult:Object;  
           
            [Bindable]
            public var qResult:ArrayCollection;

            [Bindable]
            public var aReturnTypes: Array = [ {label:"string", data:"string"},
                {label:"array", data:"array"}, {label:"struct", data:"struct"}, {label:"query", data:"query"} ];

            [Bindable]
            public var returnType:String="string";
           
            public function clearAll():void{
                sResult=new String();
                aResult=new Array();
                oResult=new Object();
                qResult=new ArrayCollection()
               
            }
           
            public function handleStringResult(event:ResultEvent):void{
                sResult=event.result as String
            }
            public function handleArrayResult(event:ResultEvent):void{
                aResult=event.result as Array
            }
            public function handleStructResult(event:ResultEvent):void{
                oResult=event.result as Object
            }
            public function handleQueryResult(event:ResultEvent):void{
                qResult=event.result as ArrayCollection;   
            }
           
            public function handleAnyTypeResult(event:ResultEvent):void{
                switch (returnType) {
                    case ’string’:
                        handleStringResult(event);
                        break ;
                    case ‘array’:
                        handleArrayResult(event);
                        break ;
                    case ’struct’:
                        handleStructResult(event);
                        break ;
                    case ‘query’:
                        handleQueryResult(event);
                        break ;
                    default:
                        handleStringResult(event);
                        break ;
                }

            }
             
        ]]>
    </mx:Script>
   
    <mx:RemoteObject
        id="myService"
        destination="ColdFusion"
        source="services.HelloWorld"
        showBusyCursor="true"
        >  
            <mx:method name="sayHelloString" result="handleStringResult(event)"  fault="Alert.show(event.fault.message)"/>
            <mx:method name="sayHelloArray" result="handleArrayResult(event)" fault="Alert.show(event.fault.message)"/>
            <mx:method name="sayHelloStruct" result="handleStructResult(event)" fault="Alert.show(event.fault.message)"/>
            <mx:method name="sayHelloQuery" result="handleQueryResult(event)" fault="Alert.show(event.fault.message)"/> 
            <mx:method name="sayHelloAnyType" result="handleAnyTypeResult(event)" fault="Alert.show(event.fault.message)"/> 
    </mx:RemoteObject>

   
    <mx:Button label="get String Remote Object" click="myService.sayHelloString()"/>
    <mx:Label id="lblStringResult" text="{sResult}"/>

    <mx:Button label="get Array Remote Object" click="myService.sayHelloArray()"/>
    <mx:List dataProvider="{aResult}" width="150"/>

    <mx:Button label="get Struct Remote Object" click="myService.sayHelloStruct()"/>
        <mx:Form >
            <mx:FormHeading label="How do you say {oResult.ENGLISH} in Latin?"/>

             <mx:FormItem>
                <mx:RadioButtonGroup id="language" />
                <mx:RadioButton groupName="language" id="lFrench" value="french"
                    label="{oResult.FRENCH}" width="150" />
                <mx:RadioButton groupName="language" id="lSpanish" value="spanish"
                    label="{oResult.SPANISH}" width="150" />
                <mx:RadioButton groupName="language" id="lLatin" value="latin"
                    label="{oResult.LATIN}" width="150" />                     
            </mx:FormItem>                        

    </mx:Form>

    <mx:Button label="get Query Remote Object" click="myService.sayHelloQuery()"/>
    <mx:DataGrid dataProvider="{qResult}">
         <mx:columns>
            <mx:Array>
                <mx:DataGridColumn dataField="hello"/>
                <mx:DataGridColumn dataField="world"/>
                <mx:DataGridColumn dataField="exclamation"/>
            </mx:Array>
        </mx:columns>
    </mx:DataGrid>
   
    <mx:HBox>
        <mx:Button label="get >" click="returnType=cbo.selectedItem.data;myService.sayHelloAnyType(returnType);"/>
        <mx:ComboBox id="cbo" dataProvider="{aReturnTypes}"/>
    </mx:HBox>
   
    <mx:Button label="clear all" click="clearAll()"/>
       
</mx:Application>
 

This movie requires Flash Player 9