Shaun Mccran

My digital playground

01
J
U
L
2008

Flex - Coldfusion Remoting, passing an Object usefully

Ever noticed that with the increase in scale of a Flex application, the amount of data that you pass around seems to grow exponentially? Well I was fed up with passing multiple values back and forth from CF to flex, so after some digging here's the first article in using objects in remoting.

I want to be able to pass an object back and forth from CF to Flex, and if arguments are present use them, otherwise don't bother. This first article is based on constructing an object in flex, and receiving it in CF.

We send an object, but that's not what we get on the other end......

The CFC


First create your cfc, we are going to use cfproperty first to declare the types of our data.

view plain print about
1<cfcomponent output="false">
2<cfproperty name="county" displayname="county" hint="" type="string" default="" required="true">
3<cfproperty name="tel" displayname="tel" hint="" type="string" default="" required="false">
4<cfproperty name="country" displayname="country" hint="" type="string" default="" required="false">

Next we'll build our function, keeping the same arguments, in the same order. None of these are required, that way if they aren't in our object, then we just get an empty string.

view plain print about
1<cffunction name="sendObject" access="remote">
2        <cfargument name="county" type="string" required="false" default="">
3        <cfargument name="tel" type="string" required="false" default="">
4        <cfargument name="country" type="string" required="false" default="">
5
6    
7    county= #arguments.county#
8    tel= #arguments.tel#
9    country= #arguments.country#
10    
11    </cffunction>
12
13</cfcomponent>

So that's our 'catching' cfc method. It uses named pairs that we will create in flex, as part of our object.

The Flex


I'm going to assume that you know how to setup a remote object call to CF.

We create a function (sendMeStuff), which builds an object, and populates three arguments. Then we fire the object at the Remote Object method. This calls the ColdFusion cfc (catchMe) invoking the method 'sendObject'.

view plain print about
1<mx:Script>
2    <![CDATA[
3        import mx.rpc.events.ResultEvent;
4        import mx.controls.Alert
5
6        private function sendMeStuff():void
7        
8        {
9        var arguments:Object = new Object();
10         arguments.county = "wiltshire";
11         arguments.tel = "0800 49 59 69";
12         arguments.country = "England";
13         // fire it at your remote object
14
RO.sendObject(arguments);
15            
16        }    
17        
18        private function handleRO(e:ResultEvent):void
19        
20        {
21            Alert.show('sent it')        
22        }
23        
24    ]]>
25</mx:Script>
26
27<mx:RemoteObject destination="ColdFusion"
28                 id="RO"
29                 fault="Alert.show(event.fault.message)"
30                 source="catchme"
31                 showBusyCursor="true"
>

32
33    <mx:method name="sendObject" result="handleRO(event)" fault="Alert.show(event.fault.message)"/>
34
35</mx:RemoteObject>
36    <mx:Canvas x="10" y="10" width="226" height="216" backgroundColor="#ffffff">
37        <mx:Button x="67" y="79" label="Click me" click="sendMeStuff()"/>
38        <mx:Text x="10" y="10" text="Click the button to send the stuff"/>
39    </mx:Canvas>

Note: We send in an object, but receive named pairs.

This seems like a much easier way of remoting than passing individual arguments about. I only have two reservations.

1. A colleague of mine has tried this through http service calls, IE as a webservice, and it doesn't seem to work. I will experiment and confirm....

2. CfProperty doesn't seem to make any difference at all! I've been investigating Strongly typing data between flex and ColdFusion and I was sure this was an example of this, but everyone else seems to think that cfProperty is crucial, anyone shed some light?

So now to work out the opposite! A cf object into Flex! (besides just setting datagrid.dataProvider = event.result)!

Related Blog Entries

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top