Following on from the cfmeetup presentation last week on "Getting Started with Adobe AIR", I received some requests to share the code used in the example applications.
In this post, you will find not only the source code to connect a Flash Builder 4 application to a remote ColdFusion component, but also a rather useful (hopefully) video tutorial to guide you on your way.
You can view the video by pressing the above link or watch it directly on YouTube.
Source Code
HelloWorld.cfc
<cfcomponent displayname="helloWorld"
output="false"
hint="I am a simple component that you can use to call
from your Flex applications.">
<cffunction name="helloWorld"
access="remote"
output="false"
hint="I return a string greeting.">
<cfreturn 'Monkeh love IS good love!' />
</cffunction>
<cffunction name="helloPerson"
access="remote"
output="false"
hint="I return a personalised string greeting.">
<cfargument name="name" required="true"
type="String"
hint="The name of the person to greet" />
<cfreturn 'Hey, #arguments.name#! Did you know that
Monkeh love is good love?' />
</cffunction>
<cffunction name="getQuery"
access="remote"
output="false"
hint="I return a query object.">
<cfset var qContacts = queryNew('firstName, lastName') />
<cfset queryAddRow(qContacts, 4) />
<cfset querySetCell(qContacts, 'firstName', 'Paul', 1) />
<cfset querySetCell(qContacts, 'lastName', 'McCartney', 1) />
<cfset querySetCell(qContacts, 'firstName', 'John', 2) />
<cfset querySetCell(qContacts, 'lastName', 'Lennon', 2) />
<cfset querySetCell(qContacts, 'firstName', 'George', 3) />
<cfset querySetCell(qContacts, 'lastName', 'Harrison', 3) />
<cfset querySetCell(qContacts, 'firstName', 'Ringo', 4) />
<cfset querySetCell(qContacts, 'lastName', 'Starr', 4) />
<cfreturn qContacts />
</cffunction>
</cfcomponent>
The main mxml file
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600"
creationComplete="remotesvc.getQuery()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<s:RemoteObject id="remotesvc"
destination="ColdFusion"
endpoint="http://localhost:8500/flex2gateway/"
source="helloWorld">
<s:method name="helloPerson"
result="helloResult(event)" />
<s:method name="helloWorld"
result="helloResult(event)" />
<s:method name="getQuery"
result="queryResult(event)" />
</s:RemoteObject>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable] private var acBeatles:ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private function helloResult(event:ResultEvent):void {
Alert.show(event.result.toString());
}
private function queryResult(event:ResultEvent):void {
acBeatles = event.result as ArrayCollection;
}
]]>
</fx:Script>
<s:TextInput id="nameInput" x="10" y="24"/>
<s:Button label="Question"
click="remotesvc.helloPerson(nameInput.text)"
x="146"
y="25"/>
<s:Button label="Statement"
click="remotesvc.helloWorld()"
x="245"
y="25"/>
<mx:DataGrid x="10" y="88"
dataProvider="{acBeatles}"></mx:DataGrid>
</s:Application>