Skip to main content

Designing CFCs for use with Flex

I have a design pattern for CFCs that I like to use, but recently I ran into an annoying problem where I was trying to use some of my already in-use CFC with a client written in flex, consuming the CFCs as web services.

By finding a nice tip on Kyle Hayes' blog (see here), I was able to get things to working just the way I had hoped, and this blog entry will show how it all comes together.

Here is the basic structure of my standard CFC  template:


<cfcomponent>


<cfset my = StructNew()>

     <cfset my.getList = structNew()/>

     <cfset
my.getList.error = 0/>

     <cftry>

          <cfquery name="qrsRESULTS" dataSource="MY_DATASOURCE">

         
SELECT col1,col2,col3

         
FROM SOMETABLE

         
WHERE somevalue = '#ARGUMENTS.somevalue#'

         
</cfquery>

          <cfset my.getList.result = qrsRESULTS>

          <cfcatch type="any">

              
<cfset my.getList.error = 1>

              
<cfset my.getList.message = CFCATCH.message>

         
</cfcatch>

     </cftry>

     <cfreturn my.getList>

</cffunction>

<cffunction name="getStuffAsQuery" returntype="struct" access="remote">

     <cfargument name="somevalue" type="string" required="true">

</cfcomponent>


Note that the return value is "Struct" even though I am wanting to return a Query object (which Flex will auto-interpret to be an ArrayCollection).  Also note that I am constructing a structure with the named "my.{function name}" where all the values are stored. 

The main reason for making your CFC this way is so that you have a predictable way to check if each and every method call succeeds and a predictable way to get the results.  You can check the .error value of every call for success, and in your application (whether it be CF or Flex), take the appropriate error-handling steps.

In Flex, you would define your webservice for the above CFC like so:


<mx:WebService id="wsdlService1" wsdl="http://www.host.comspam/stuff.cfc?wsdl" showBusyCursor="true">

     <mx:operation name="getStuffAsQuery" result="handleMyResult(event)" fault="errorHandler(event)">

         
<mx:request xmlns="">

              
<somevalue>{this you have to provide}</somevalue>

         
</mx:request>

     </mx:operation>

</mx:WebService>


To execute this web service call, you would do the following:

     wsdlWhitelist.getListAsQuery.send()

When this call completes successfully, it will call the "handleMyResult()" function, and if it fails, it will call the "errorHandler()" function (see above code for these references).

Lets look at the "handleMyResult()" function:


 

public function handleMyResult(event:ResultEvent):void

{

      if (event.result.ERROR == 0)

      {

            Alert.show("Data received properly!");

            // query results are in variable "event.result.RESULT"

            // Flex will automatically interpret this as an ArrayCollection

            // so you can assign it as the data provider for a DataGrid, etc.


      }

      else

      {

            Alert.show("There was an error loading data: " + event.result.MESSAGE);

      }

}

In this function, the result event is passed in.  "result.event" contains whatever came back from the web service, which in our case is a ColdFusion struct.  Flex will interpret this as an untyped Object, just like a Flash Object.  The struct members from ColdFusion are there, but are object properties now.  The important thing to note here is that the names of the object properties that came from our ColdFusion struct are now all caps.  In the CFC code, we had a property called "error" -- but in Flex, we have to reference that as "ERROR."  Likewise, the RESULT and the MESSAGE properties are from our ColdFusion struct.

More later, and if you have questions, feel free to ask.

Popular posts from this blog

Making Macbook Air with 128GB SSD usable with Bootcamp

I recently got a new Macbook Air 11" (the 2012 version) and loaded it with goodies like 8GB ram and 2GHz Core i7.  What I DIDN'T upgrade was the internal SSD.  My config came with 128GB SSD and I refused to pay $300+ to upgrade it to 256GB.  Yeah I know, some call me cheap, but SSds cost $75-$150 for 240GB, so adding another 128GB for $300 seemed way too steep for me.  I figured "ok, I'm going to make 128G work!"

Here is the story of how that went...

Installing python 3.4.x on OSX El Capitan

I love "brew" package manager, but sometimes being too progressive breaks things.  I have several python apps that I maintain that get deployed to AWS using Elastic Beanstalk.  AWS eb can deploy with python 2.7 or 3.4.  Any recent 'brew install python3" will get 3.5.1. #annoying

Dell XPS M1330 + Snow Leopard Hackintosh

I have been working with a Dell XPS M1330 laptop for a few years now.  It doesn't quite match up to the newest notebooks in terms of performance, but it certainly still has some life in it.  I had previously installed OSX 10.5.x on it as an experiment, and had moderate success.  I decided to revisit this idea again to install Snow Leopard (OSX 10.6) on the Dell M1330, and keep some notes for those of you brave enough to Hackintosh your own machine...