About a week ago I exchanged a few emails with Susan. She was learning Flex, and while she thought she understood the basic UI stuff (layout, widgets), she was having a real hard time grokking the basics of using ColdFusion Components with Flex. I whipped up a quick demo for her that was as simple as possible and I thought I'd share it on the blog. Please note that Flex, like ColdFusion, provides multiple ways of doing things, so this code could probably be done in many other ways.
My blog entry will make use of Flex Builder 3. The UI will be a bit different if you are using Flash Builder 4. To begin, I created a simple project named TestB. I specified "Web" for Application Type, and most importantly, selected "ColdFusion" at the bottom in the Application Server Type. Notice I've got "Use remote object access service" is checked and I've selected "ColdFusion Flash Remoting." (Most of this was selected for me, but I don't know if that is simple FB remembering for me.)

Click next. Now on the next page, a lot will depend on your ColdFusion setup. Mine is a standalone server tied to Apache.

Click Finish (or Next and Finish) and you should be presented with a (mostly) blank MXML file.
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4 </mx:Application>
Ok, so for my example, I didn't want to worry about input and output. Instead, I just want to call a CFC that returns a string. Using my ... um... other editor... I created a very simple CFC named testa in web root:
2
3 <cffunction name="echotest" access="remote" returnType="string">
4 <cfreturn "The time is #timeFormat(now())#">
5 </cffunction>
6
7 </cfcomponent>
I've got one method, echotest, that simply returns a sentence with the time. Note: The method is marked remote. I believe you can setup ColdFusion to allow Flash Remoting calls to methods marked public, but I don't think I'd do that. It's simpler, and more direct (IMHO) to mark my methods remote that I want exposed to Flex.
I saved the CFC and ran it in my browser just to ensure I didn't do anything stupid:
http://localhost/testa.cfc?method=echotest
Ok, now back in Flex Builder. If you read the docs (Accessing Server-Side Data with Flex) you will see that there are a few ways to access data with Flex. The best way, as far as I know, will almost always be Flash Remoting. This is done with the RemoteObject component. The tag lets you define a Flash Remoting connection to a CFC, like so:
2 <mx:method name="echotest" fault="handleFault(event)" result="handleResult(event)" />
3 </mx:RemoteObject>
In the code above I've:
- Specified ColdFusion as my destination, which will be routed through the servers FlashRemoting connection
- Specified a "dot path" address for my CFC. My file was called testa.cfc. If you were to create an instance of that using createObject, you would just use "testa". If the CFC was in a folder called services, my path would be services.testa. You get the idea. Since the CFC is in web root, I can just use testa.
- Finally, the ID, cfservice, just let's me get a handle to the service.
Next up, let's create a simple UI to run my test. I'll add a button and a textarea to store the result from the server:
2 <mx:TextArea id="resultBox" />
(I won't go into detail on each line of code. I assume, like Susan, that you either know Flex enough to get what's going on, or can guess. So for example, in the above code, the first line makes a button, and the second a textarea.)
The final bit is to tie is all together. Notice my button runs a method on the cfservice RemoteObject. In this case, it isn't a method I wrote in ActionScript, but the method in the CFC. Also remember this line from above:
This said: For the service, when echotest is run, do handleFault() on an error and handleResult on a normal result. I need code for these, and here is what I used:
2 <![CDATA[
3 import mx.controls.Alert;
4
5 function handleFault(evt) {
6 mx.controls.Alert.show(evt.fault.faultString)
7 }
8
9 function handleResult(evt) {
10 resultBox.text = evt.result
11 }
12 ]]>
13 </mx:Script>
The fault handler simply displays the error. The result handler though takes the result data and adds it to the text area. How did I know "result" was a key in evt? I didn't. I debugged it and played around with it. Basically evt is a Result object and the result key is the 'pure' result. (My take on it - I'm sure folks can say that a lot better.)
Altogether now, my MXML looks like so:
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
3
4 <mx:RemoteObject destination="ColdFusion" source="testa" id="cfservice">
5 <mx:method name="echotest" fault="handleFault(event)" result="handleResult(event)" />
6 </mx:RemoteObject>
7
8 <mx:Script>
9 <![CDATA[
10 import mx.controls.Alert;
11
12 function handleFault(evt) {
13 mx.controls.Alert.show(evt.fault.faultString)
14 }
15
16 function handleResult(evt) {
17 resultBox.text = evt.result
18 }
19 ]]>
20 </mx:Script>
21
22 <mx:Button label="Talk to ColdFusion" click="cfservice.echotest()" />
23 <mx:TextArea id="resultBox" />
24
25 </mx:Application>
Running it, and clicking the button of course, gives us:

Woot. That's a fancy looking clock there, eh? Ok, so let's take it a step further and try an example with input and output. Back in my CFC, I added a new method:
2 <cfargument name="name" type="string" required="true">
3 <cfreturn "Hello, #arguments.name#!">
4 </cffunction>
The hellotest method takes a name argument and returns a string including the name. Back in Flex Builder, go and make a new file. (I selected MXML Application.) I could have modified the previous page, but I wanted to keep things simple. This time my RemoteObject test will contain a method for the hellotest call.
2 <mx:method name="hellotest" fault="handleFault(event)" result="handleResult(event)" />
3 </mx:RemoteObject>
You do not have to define a method for each CFC function you want to call. I'm using it as a convenience as it allows me to define the fault/result handler. My UI now is a bit more complex. I have a form field to enter your name, a button, and a result textarea:
2 <mx:Button label="Say Hello" click="cfservice.hellotest(nameText.text)" />
3 <mx:TextArea id="resultBox" />
Notice that the button grabs the value from the name field. The ActionScript is the exact same, so here is the complete MXML file:
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
3
4 <mx:RemoteObject destination="ColdFusion" source="testa" id="cfservice">
5 <mx:method name="hellotest" fault="handleFault(event)" result="handleResult(event)" />
6 </mx:RemoteObject>
7
8 <mx:Script>
9 <![CDATA[
10 import mx.controls.Alert;
11
12 function handleFault(evt) {
13 mx.controls.Alert.show(evt.fault.faultString)
14 }
15
16 function handleResult(evt) {
17 resultBox.text = evt.result
18 }
19 ]]>
20 </mx:Script>
21
22 <mx:TextInput id="nameText" />
23 <mx:Button label="Say Hello" click="cfservice.hellotest(nameText.text)" />
24 <mx:TextArea id="resultBox" />
25
26 </mx:Application>
Running it, and entering a test value, gives me the following:

Enjoy. If folks want to post comments to other Flex+ColdFusion blog entries like this, I'd definitely appreciate it, and I'm sure Susan would as well.
Next - I'll show this in FlexFlashBuilder 4 and how the cool new server side introspection stuff works. (Hint: It's the bee's knees.)


Comment 1 written by marc on 3 June 2009, at 8:26 PM
there are a number of articles/posts out there showing how you can set this all dynamically without the need to modify this file, but for quick and dirty getting set up, I think you need to do this.
Comment 2 written by Raymond Camden on 3 June 2009, at 9:13 PM
Comment 3 written by Dale Fraser on 3 June 2009, at 9:14 PM
Comment 4 written by Gareth Arch on 3 June 2009, at 9:20 PM
Comment 5 written by Tariq Ahmed on 4 June 2009, at 12:10 AM
http://www.cflex.net/Tutorials/cfandflex.cfm
Comment 6 written by Raymond Camden on 4 June 2009, at 8:15 AM
Comment 7 written by Brian on 4 June 2009, at 9:40 AM
Comment 8 written by Paul on 4 June 2009, at 11:05 AM
Comment 9 written by Raymond Camden on 4 June 2009, at 12:28 PM
Comment 10 written by Ken Caldwell on 4 June 2009, at 5:54 PM
I find it quite ironic and insulting that with the constant (CF is dead) chatter, that Adobe would only have examples of this using Java.
I even posted a comment on Ben Forta's blog about this and did not get a reply.
Ken
Comment 11 written by Raymond Camden on 5 June 2009, at 7:06 AM
As for Ben, remember, bloggers always can't answer every request. :)
Comment 12 written by John on 5 June 2009, at 9:20 AM
I do have one question, how would you configure FlexBuilder to handle a remote CF server where you don't have a mapping to it on the machine you are working? Would I have to install a dev version of CF on my machine and then be sure all the configuration options mirror the production environment?
Comment 13 written by Raymond Camden on 6 June 2009, at 9:29 AM
Comment 14 written by Susan Brun on 8 June 2009, at 10:16 AM
I wish Adobe would put some better documentation on their site.
Comment 15 written by Raymond Camden on 8 June 2009, at 10:19 AM
Comment 16 written by Craig on 8 June 2009, at 5:45 PM
<fx:Declarations>
<s:CallResponder id="getGridResult"/>
<validationfunctions:ValidationFunctions id="validationFunctions" destination="ColdFusion" endpoint="http://localhost:8500/flex2gateway/" fault="Alert.show(event.fault.faultString)" showBusyCursor="true" source="PTS-debug.cfc.validationFunctions"/>
This radically reduces code and seems easier than the previous remoteobject tag model.
BUT... I can't figure out how to get access in actionscript to a returned string. All the functions that bind to a control work fine. However, I'm trying to access a string in actionscript generated in my cfc.
My component looks like this:
<cfcomponent>
<cffunction "returnAString" access="remote" returntype="string">
<cfreturn "Heres the string">
</cffunction>
</cfcomponent>
I can't figure out how to get at the cfreturn result in actionscript
Comment 17 written by Ken Caldwell on 8 June 2009, at 5:50 PM
Thanks for the reply. I looked at your game example, you use spring. I was just after a quick, plain Flex, CF, Blazeds example. Just something like a maintence form for the artists table. Using consumer and producer. I want to put together a proof of concept for my manager. I don't have the time to learn spring or any other bridge. I went to a number of Adobe events and the reps always show this and say how easy it is. But there are never any examples.
As for Ben not replying. He is the senior Adobe Evengelist. I would think it is his job to reply to concern like I have. We have an environment where there are daily posts about CF is dead and the like. When Adobe does not even supply examples using their technology, I believe this only inflames the problem.
Ken
Comment 18 written by Raymond Camden on 8 June 2009, at 6:14 PM
@Craig: Sorry - no idea. I was planning on doing the FB4 version today. If I had to guess, the object, validationfunctions, is roughly the same as your RemoteObject, but it is named oddly.
Comment 19 written by Raymond Camden on 8 June 2009, at 6:44 PM
Comment 20 written by Racquel Tejano on 23 October 2009, at 12:58 PM
[Add Comment] [Subscribe to Comments]