22 March 2010
In order to successfully complete this tutorial, be sure to set up and test your environment as outlined in Getting started with ColdFusion and Flash Builder 4.
You can skip the steps above if you have already successfully completed the "Getting started with ColdFusion and Flash Builder 4 " tutorial or any other tutorial in this series.
This tutorial was intended for:
All
As an experienced ColdFusion developer, you have worked with URL, Form, and other types of variables to dynamically update data in your applications in response to user interactions. Using the concepts in this tutorial, you can easily embed your Flex application code in a CFM page and pass ColdFusion variables to it that will appropriately update the display.
In this tutorial, you will build the Fictitious Quarterly Sales Planner application shown in Figure 1. After you set up the database and review the provided CFC, you will lay out the ColumnChart control using Adobe Flash Builder 4. Next, you will bind data to the control from a service operation that is mapped to a ColdFusion component (CFC) function.

The header of the application (with the logo, welcome message, and region tabs) is built with ColdFusion, HTML, and cascading style sheets (CSS). As a user clicks each region tab, he will pass a URL variable to the application, which will refresh the chart display with that region's information. Note that the figure shows the specified region variable in the URL string.
This tutorial does not attempt to replace ColdFusion charts with charts built using Flash Builder 4. You will want to use Flash Builder for much more interactive displays that cannot be created using ColdFusion alone. However, for simplicity's sake, you are building a straight-forward chart example to strip away any complexities in learning how to embed the SWF file into a CFM page and how to pass URL variables into the HTML embed code that, in turn, passes the variables into the application.
You can skip the steps above if you have already successfully completed the "Getting started with ColdFusion and Flash Builder 4 " tutorial or any other tutorial in this series.
This tutorial was intended for:
As mentioned in the introduction, this tutorial uses the same development environment as the Getting started with ColdFusion and Flash Builder 4 tutorial and the other tutorials in this series. In this section, you will name a second data type for use with a different service call to a CFC function.

If you do not see the data type, follow the steps below to assign your own data type name to the returned data that is retrieved in the getAllData() service call to the CFC method.

Each record from the query has been placed in an ActionScript object with each column of data (FIRSTNAME, LASTNAME, EMAIL, and so on) set in a name/value pair. Each record is indexed starting at zero.
Note: Unlike ColdFusion, ActionScript is a zero-indexed language.
Rather than generically referring to this returned data as an object, this wizard lets you give the data a return type name. You will do this next.



You will see the EmployeeDataByRegion data type listed in the Data Types category of the Data/Services view (see Figure 7). Note that the properties of the data type are the query columns returned from the CFC method.

The data returned from the CFC contains employee information that is associated with a region: Northwest, Southwest, Central, Northeast, and Southeast. Later in this tutorial, you will learn more about passing dynamic data to Flash applications. For now, you will hard-code the region value.
In this section, you will bind the returned results of the getEmployeeDataByRegion() operation to a Flex framework ColumnChart control. You will not need to write any code to accomplish this; instead you will use a built-in Flash Builder wizard to generate all the code.
In this section, you will use the drag-and-drop capabilities in Flash Builder Design mode to add your ColumnChart component.
Note: In practice you will usually have only one main application file in a Flash Builder project. You will create a new main application file for each tutorial you follow in this series to avoid creating additional Flash Builder projects.



The ID property is the unique identifier for this ColumnChart control and can be used by the application, and you, for programmatic manipulation of the component. The chart will display the year-to-date sales amount sold by the employee.
Note: You may need to click the Refresh button to the right of the Design mode to display the chart. You can also double-click the Editor tab that contains the MXML file name to expand and collapse the design area.
If the legend shows up in the upper left corner of the Design area, drag and drop it next to the ColumnChart control.
In this section you will bind data to the ColumnChart control.

You should see the Bind To Data dialog box (see Figure 12). Note the table below the Configure chart heading with the two columns: Series and Y Field.
You'll see a popup list with all the properties from the getEmployeeDataByRegion() service operation.
The Y Field is the Y-axis property of the ColumnChart control. When you select the data field in the popup list, it will be displayed along the vertical axis for the chart.
The x axis field is the X-axis property of the ColumnChart control. When you select the data field in the popup list, it will be displayed along the horizontal axis for the chart.

You will be switched to Source mode with the region attribute highlighted in the getEmployeeDataByRegion() function call of the ColumnChart control click handler function (see Figure 13). Remember that the region attribute is an expected value of the CFC function.

'Central'. Remember that you are hard-coding this value initially in the tutorial. You will learn how to pass dynamic data using this variable later in the tutorial.

You should see the ColumnChart control with YTD values in the Y-axis and Employee name in the X-axis (see Figure 15).

In this section, you will integrate the generated HTML wrapper code created by Flash Builder with HTML and CSS code in a ColdFusion web page.
In this section, you will learn about how to turn the HTML wrapper file into a CFM file that displays a custom header.

By default, Flash Builder is set to generate an HTML wrapper file for the compiled SWF file.

The html-template directory contains files that will be used to generate the HTML wrapper code for the compiled SWF file.
This file is the generated HTML wrapper code.
Note: For more information about the HTML wrapper file and its supporting files, see Deployment assets.
Make sure that the CompanyHeader.cfm file is at the same level as the F4CF_Deploying_Flex.html file and that the assets folder is directly inside of the F4CF_Getting_Started-debug folder.
Figure 18 shows the code that embeds the compiled SWF file for the application.

You should see the ColumnChart shown in Figure 19.

In Flash Builder, select the Project menu. By default, the Build Automatically menu item is selected. This means that every time you make a change to your MXML or ActionScript code and save your files, the SWF file will be recompiled and the HTML wrapper file will be re-generated. You can ignore the new HTML file that will be generated throughout the rest of this tutorial.
Remember that this chart will show the sales numbers for employees by region. In this section, you will add a header that displays the regions as links that self-reference this CFM page.
Earlier in this tutorial, you hard-coded the region value in the data service call to the Central region. In this section, you will also pass a URL variable in the region links for use in the application.
<body> tag around line 57, include the CompanyHeader.cfm file using the cfinclude tag as shown in the following code:<cfinclude template="CompanyHeader.cfm">
This will add a header to the ColdFusion page and show the five regions as links.
You will see a page with a ColumnChart and region hyperlinks (see Figure 20).

If you click the NorthWest Region hyperlink, you will notice that the page reloads and that the url parameter of region=Northwest is added to the query string in the URL. The other hyperlinks will also load the page with the corresponding name/value pair for the region. This is accomplished by the code in the CompanyHeader.cfm:
<li id="Northwest" <cfif url.region eq "Northwest">class="current-page" </cfif>><a
href = "F4CF_Deploying_Flex.cfm?region=Northwest">NorthWest Region</a></li>
In the following steps, you will apply the provided stylesheet to the display to format the tabs.
<style> tag block around line 21.text-align property to left as shown in the following snippet:<style type="text/css" media="screen">
html, body{ height:100%; }
body { margin:0; padding:0; overflow:hidden; text-align:left; }
#flashContent { display:none; }
</style>
You will see that the bulleted list of links is aligned to the left of the page instead of to the center.
<style> tag block, add the following code to use the style sheet provided with the sample files for this tutorial.<link rel="stylesheet" href="assets/stylesheet.css" type="text/css"/>
You will see the formatted tabs shown in Figure 21.

In this section, you will learn how pass the URL variables into the application.
As an experienced ColdFusion developer, you have used URL variables in your application development and you know that the URL variables can be printed to the HTML content. In this case, you will print the URL variables into the JavaScript code that embeds the SWF file.
swfobject.embedSWF() method call to embed the SWF file:swfobject.embedSWF(
"F4CF_Deploying_Flex.swf", "flashContent",
"100%", "100%",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
Note that an object named flashvars is one of the arguments in the method call. You can create custom name/value pairs in this object to pass to the application from this embed code.
<script> tag block: var flashvars = {};
<cfoutput>
flashvars.region="#URL.region#"
</cfoutput>
This code creates a property named region in the flashvars object with a value that is dynamically set to the region variable that is passed in the URL query string.
Earlier in this tutorial you had hard-coded the value for the region to Central. Now that you are passing the region into the application via the flashvars object, you can access the property and use it to update the ColumnChart control.
<s:Application> tag, add the initialize event to call a function named init, as shown in the following snippet:<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/halo" minWidth="1024" minHeight="768"
xmlns:f4cf_getting_started_service="services.f4cf_getting_started_service.*"
initialize="init()"
>
The initialize event is a system event that is triggered when the application loads. You will create a function, named init(), that will be run every time the application loads.
<fx:Script> tag block and below the import statement, create the init() function using the keywords private and function:<fx:Script>
<
Remember that a new SWF file will be compiled when you save the file and will be placed in the F4CF_Deploying_Flex-debug folder in your ColdFusion webroot.
Now, when you click the region tabs, you will see data from that region.
In this tutorial you learned about the HTML wrapper file, the flashvar object, and how to use these elements to integrate your compiled Flex application with ColdFusion code and how to pass URL variables into the SWF files.
For more information on how to use style sheets and details on the tab implementation, please refer to the following links:
To learn more about flashvar objects, refer to Peter deHaan's Flex Examples blog.