Important note: Effective with the release of Adobe LiveCycle ES, the Adobe Flex Data Services 2 server product has been rebranded as a Solution Component of LiveCycle ES. This article was written based on Flex Data Services but will likely work as is with LiveCycle Data Services ES. Any articles referring to or using ColdFusion and Flex Data Services are not compatible with LiveCycle Data Services ES. To learn about the new capabilities of LiveCycle Data Services ES, see the tutorials in the LiveCycle Developer Center and read about Adobe LiveCycle Data Services ES.
Adobe ColdFusion MX 7.0.2 enables you to create applications with the rich Internet capabilities of Adobe Flex and the advanced data retrieval features of ColdFusion. You can use ColdFusion to create, read, update, and delete records in a database while using the rich presentation features of Flex. This article is part of a series of articles that demonstrate how you can use ColdFusion with Flex.
This article uses the Flex Phone Selector sample application to demonstrate some basic Flex funcitonality, including:
The Phone Selector application is a very simple example of a Flex application powered by ColdFusion. This sample application is deliberately small and highly focused to demonstrate ColdFusion Flex integration options, including:
This application is not intended to demonstrate development best practices, and was designed purely to demonstrate integration capabilities, functionality, and techniques.
Note: This application does not require Flex Data Services 2.0, nor does it require any data sources or setup within ColdFusion.
To make the most of this article, you need to install the following software and files:
Important note: Effective with the release of Adobe LiveCycle ES, the Adobe Flex Data Services 2 server product has been rebranded as a Solution Component of LiveCycle ES. This article was written based on Flex Data Services but will likely work as is with LiveCycle Data Services ES. Any articles referring to or using ColdFusion and Flex Data Services are not compatible with LiveCycle Data Services ES. To learn about the new capabilities of LiveCycle Data Services ES, see the tutorials in the LiveCycle Developer Center and read about Adobe LiveCycle Data Services ES.
Familiarity with ColdFusion data management and general Flex concepts.
The phone selector application has two parts: the ColdFusion code (ColdFusion components and a test ColdFusion page) and the Flex code (MXML and AS files). Make sure the ColdFusion code works properly before testing any integration.
To install the application, follow these steps:
Create a folder named Phones in the CFIDE/samples folder (under the web root). If a folder named samples does not exist under your CFIDE folder, create it.
Note: You must place the application (both the ColdFusion and Flex bits) under the Web root. Furthermore, for automatic conversion between CFCs and ActionScript objects to occur, you must explicitly specify the path to these components and the path must be consistent in both ColdFusion and ActionScript code.
To use Flash Remoting, specify the following Flex compiler flag in the project properties:
--services=C:\CFusionMX7\wwwroot\WEB-INF\flex\ services-config.xml
(You must change the path, if your ColdFusion is installed in a different location).
The ColdFusion back end consists of three files:
Get and Set functions for use with those properties, and a function used to populate these properties. It also contains a series of CFML <cfproperty> tags, which define the object properties. Although <cfproperty> tags are not generally used within ColdFusion applications, they are very important when creating objects that are consumed by Flash Remoting (or web services, for that matter).loadData() which is called by the constructor). The getPhones() function returns that array.The Flex Client consists of five files:
The Phone Selector application demonstrates some important concepts and techniques, which are outlined in the following sections.
The ColdFusion code consists of the following components and function:
<cfproperty> tag to list each property in this CFC. The properties, their names and types, and their exact order are extremely important. If these do not match the properties in the equivalent ActionScript file, mapping does not occur (and when the array phones arrives in Flex it is an array of generic objects instead of an array of Phone objects).<cfinvoke> tag to create and populate a Phone.cfc (which is returned and appended to the array). The <cfinvoke> tag uses a fully qualified path to Phone.cfc, even though it is in the same folder. (In fact, component="Phone" would work). This, too, is to facilitate CFC to ActionScript class mapping. The fully qualified path is embedded in the array that is sent to the Flex application; it must match the path specified in the equivalent ActionScript class.getPhones(): The getPhones() function is the only function invoked from Flex. As a result, it must specify access="remote". Only CFC methods with this access level can be invoked through Flash Remoting.The Flex code includes the following files:
Main.mxml:
<mx:RemoteObject> tag specifies to Flex how to get to the ColdFusion Catalog.cfc file. The source is fully qualified, and the destination is ColdFusion. This generic destination makes it unnecessary to map specific endpoints in Flex XML configuration files.<mx:Panel> tag with a <mx:ControlBar> tag at the bottom.<mx:Application> tag contains creationComplete="svc.getPhones()", which specifies that the application automatically invokes the ColdFusion getPhones() function as soon as the application is loaded and ready to use.<mx:RemoteObject> tag. It creates an array that contains the data and uses that array to populate the phones variable.Phone.as:
[RemoteClass(alias="CFIDE.samples.Phones.CF.Phone")]<cfinvoke> syntax in Catalog.cfc, which is how the object types are automatically matched to each other.toString() method. (It is a good idea for every class you create to have a toString() method). In this simple example there are no other methods; however, there could be, just as the equivalent Phone.cfc has extra functions.FeatureFormatter.as:
<cfif> tags to determine whether to display values, and how to display them. This is a little more complicated in a Flex application; it is a custom formatter.format() method builds the string, starting with the two highlight points and conditionally appending additional text.ProductDetails.mxml:
phone of type Phone is defined. This is passed in Main.mxml.Flash Remoting is the most efficient way to pass data between ColdFusion and client-side Flex applications. An important part of the new Flash Remoting adapter is support for automatic CFC to ActionScript object mapping. This requires careful calling conventions, explicit property definitions, and a RemoteClass definition within the ActionScript class. Once these are in place, passing complex data back and forth becomes very easy, and object types are retained (making any CFC functions or ActionScript class methods usable).
For more information, visit the Flex and ColdFusion topic page in the Flex Developer Center.
Using ColdFusion with Flex – Part 4: Creating and running a session tracker