Article

Supercharge Your Flex App with ColdFusion Power

Page: 1 2 3 4

SOAP

Starting with the SOAP web service, we’ve made the echo method a web service method by setting the attribute access="remote" in the cffunction tag. At the same time, this setting will enable us to call the method as a remote object. We’d like to retrieve the data in the WSDL format, which can be accomplished simply by adding ?WSDL to the end of the URL.

To grab that WSDL in Flex, we start off using a mx:WebService, which is structured in much the same manner as a mx:HTTPService:

<mx:WebService    
 id="dataWS"    
 wsdl="http://example.com/service.cfc?WSDL"    
 result="onDataServiceResult(event)"    
 fault="onDataServiceFault(event)"    
/>

With web services you’ll quite often find that a service offers multiple methods, and that you’d prefer to specify a result or fault handler method per service. That’s possible when using mx:operation:

<mx:WebService    
     id="dataWS"    
     wsdl="http://example.com/service.cfc?WSDL"    
     fault="onDataServiceFault(event)"    
   >    
       <mx:operation name="echo" result="onResultEcho(event)" />    
   </mx:WebService>

Providing parameters to web service calls works in a similar way to providing those to HTTP service calls—via mx:request—but may also be done by using a syntax familiar to developers coming from languages like Java or C++:

dataWS.echo("Hello")

Action Message Format

Now, let’s try this with the Action Message Format (AMF). AMF is a protocol that’s been around for quite a while, and AMF3’s specification was publicly released by Adobe in December 2007. On the server side you’d need an AMF3-enabled service—luckily, as a ColdFusion developer you’re ready to provide this type of service right out of the box.

In Flex we can use the mx:RemoteObject tag to communicate with remote objects:

<mx:RemoteObject    
 id="dataRS"    
 destination="ColdFusion"    
 source="example.service">    
   <mx:method    
     name="echo"    
     result="onResultEcho(event)"    
     fault="onFaultEcho(event)"    
 />    
</mx:RemoteObject>

Comparing this code with the equivalent web service declaration in Flex, there are a few small differences: we’re dealing with a destination named “ColdFusion” now, and mx:operation has changed to mx:method.

When one first sets up a Flex project for ColdFusion in Flex Builder, the setup wizard will ask a bunch of questions regarding the location of the ColdFusion server, its port, URL, and context root. That information provided will then be used at the compile time of the Flex application to provide Flex with the details of your AMF URL. This works smoothly and easily for simple data types such as strings or Booleans, or even built-in ColdFusion types such as arrays and structures.

You may even wish to deal with entire objects from your application’s business domain, and transfer them back and forth. There’s a design pattern called data transfer object or value object that describes such a process, and it’s fairly simple to use this method with ColdFusion components. Your service configuration in Flex will mostly remain the same, but instead of sending or expecting simple types from your remote object call, it will be a domain object: an object that represents a customer, shopping cart, employee, or whatever it is you plan to deal with. To allow Flex to deal with such complex object types, ColdFusion has to know about them as well, and there would have to be an equivalent component in ColdFusion. Here’s a CFC that describes a user:

<cfcomponent displayname="User" output="false">    
  <cfproperty name="user_id" type="numeric" />    
  <cfproperty name="user_name" type="string" />    
   
  <cffunction name="init" access="public" returntype="example.User" output="false">    
     <cfargument name="user_id" type="numeric" required="false"  />    
     <cfargument name="user_name" type="string" required="false"  />    
   
     <cfset this['user_id'] = arguments.user_id />    
     <cfset this['user_name'] = arguments.user_name />    
   
     <cfreturn this />    
  </cffunction>    
</cfcomponent>    

In the following minimal ActionScript 3 class, we also describe what a User object ought to contain:

[Bindable]    
[RemoteClass(alias="example.User")]    
public class User    
{    
       public var user_id:int=0;    
       public var user_name:String="";    
}

The metadata tag [Bindable] allows User to be used in Flex data bindings. [RemoteClass(alias="example.User")] maps the CFC type example.User on the server end to the User class here in ActionScript.

Now What?

Where do we go from here? If you’re only using ColdFusion, my guess is that you’ll find it’s easier to just use RemoteObjects for these kinds of projects. If you have a mixed environment comprising multiple technologies, it might be worth looking into web services and HTTP service calls to load XML. Either way, you’re now equipped to deal with either situation: time to have fun!

Think you're ready to kick butt with ColdFusion? Why not test your newfound knowledge with our quiz—just five easy questions, and all the answers were right here in this article. The first 100 people to take our quiz will win a copy of Getting Started with Flex delivered straight to your door absolutely free. Grab yours!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: