Accessibility

Using ColdFusion with Flex – Part 3: Creating and running a phone selector

Ben Forta

forta.com

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.

Requirements

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.

Flex Builder 2 (SDK Included)

Flash Player 9

ColdFusion MX 7.0.2  (Includes connectivity for ColdFusion with Flex 2)

Sample files:

Prerequisite knowledge

Familiarity with ColdFusion data management and general Flex concepts.

Additional documentation

Installing and configuring the application

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:

  1. Download the Phone Selector sample files that accompany this article.
  2. 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.

  3. Create two folders in the Phones folder, one named CF (for the ColdFusion code) and the other named Flex (for the Flex Project).
  4. Create a new project in Flex Builder 2. Select ColdFusion Flash Remoting Service as the server technology. Use the default location of the Flex Data Services server. Specify Phone selector as the application name. Specify the samples/Phones/Flex folder as the project location. Specify Main.mxml as the main page.
  5. 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).

  6. Right-click the project and select Close Project.
  7. Extract the ColdFusion files (catalog.xml, Phone.cfc, Catalog.cfc, and test.cfm) to the samples/Phones/CF folder.
  8. Extract the Flex files (Main.mxml, Thumbnail.mxml, ProductDetails.mxml, FeatureFormatter.as, and Phone.as) to the samples/Phones/Flex folder. When asked whether to overwrite Main.xml, click Yes to overwrite the auto-generated Main.mxml with the complete one.
  9. Copy the assets folder (with all of its contents) to the Flex folder.
  10. To ensure that the CFML code works (which you must do before attempting to connect to it using a Flex client), run test.cfm, which is in the samples/Phones/CF folder. To run test.cfm, browse to it using http://localhost:8500/CFIDE/samples/Phones/CF/test.cfm. (If you are not using the built-in server, change the URL accordingly.) Test.cfm simply instantiates the catalog, gets the phones array, and dumps the output. If test.cfm runs correctly, you are ready to try the Flex client.
  11. Open the project in Flex Builder and run it to display the phone list that ColdFusion returns. You can also select Show Details and Hide Details as needed.

The ColdFusion back end

The ColdFusion back end consists of three files:

The Flex client

The Flex Client consists of five files:

Understanding the application

The Phone Selector application demonstrates some important concepts and techniques, which are outlined in the following sections.

The ColdFusion code

The ColdFusion code consists of the following components and function:

The Flex code

The Flex code includes the following files:

Where to go from here

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

About the author

Ben Forta is the Adobe senior product evangelist and the author of numerous books, including ColdFusion Web Application Construction Kit and its sequel Advanced ColdFusion Application Development, as well as books on SQL, JavaServer Pages, WAP, Windows development, and more. Ben co-authored the official ColdFusion training material, the certification tests and Macromedia Press study guides for those tests, and now spends a considerable amount of time lecturing, speaking, and writing about application development worldwide. Visit Ben's blog to read his regular postings on ColdFusion and more.