Understanding the Link Between Flash MX and ColdFusion MX

 < Day Day Up > 



Prior to the merger of Macromedia and the Allaire Corporation (the creators of ColdFusion), there was no easy means of exchanging data between a Flash movie and a ColdFusion application. Still, developers recognized the inherent power in using the two programs together and came up with their own methods for doing so. These included elaborate ActionScript procedures, using XML documents as "translators," and other techniques that, while functional, were time-consuming to create and maintain.

But starting with the release of Macromedia Studio MX, the two programs communicate with one another with a series of tools collectively called Flash Remoting. Using Flash Remoting, Flash MX movies can display the output of a ColdFusion MX query, for example, and they can also send complex data back to a ColdFusion MX application for processing. This union gives Flash designers the ability to work with dynamic data and ColdFusion MX developers the ability to create user interfaces that go far beyond the capabilities of standard HTML.

In general, applications that employ both Flash MX and ColdFusion MX are often considered in terms of display elements and logic elements. Display elements are those that display information to a user and are usually constructed in Flash MX. Logic elements are the parts of the application that perform computations, retrieve data from a database, and so on, and these are usually constructed in ColdFusion MX. Although there are no hard-and-fast definitions for either, it's good practice to design your integrated applications with a clear understanding of which program handles each of your application's various functions.

If you've worked with variables in previous versions of Flash, you know that it has the capability of reading a text file containing simple variable values:

first_name=Matt&last_name=Hogan

A Flash movie can then use those variables as part of its display, for example, to show a "detail" page for a single user. But what about a list of users that changes daily? In this case, there is more than one defined pair of first_name and last_name variables, and the Flash movie must be prepared to display anywhere from one to possibly thousands of first name/last name combinations.

The solution lies in complex-variable types. Remember that some special types of variables can hold more than a simple numeric or string value. In Chapter 51 you learned about list variables and how a single list variable is used to store multiple values. Flash Remoting can use even more powerful variable types to pass complex data between ColdFusion and Flash. These may include structures, arrays, queries, scalars, and components. You learn more about some of these types as you proceed through this chapter.

In ColdFusion, the variable types most often used when communicating with Flash are stuctures and arrays. When a ColdFusion structure is passed to Flash, Flash treats it as an object or an associative array.

ColdFusion's structure variables can hold more than one value, but they go a step further by giving each value a key to identify it. Array variables are even more powerful, because they allow for multiple dimensions, meaning that there can be more than one value for say, first_name or last_name within a single array variable. You find more information on structures and arrays in the two sidebars accompanying this chapter.

start sidebar
Understanding ColdFusion MX Structures

Structures are special ColdFusion variables that can hold multiple values. They employ a key to refer to an item by a name. The following code defines a structure called user within a ColdFusion template:

<!---create a new structure called "user"---> <cfset user=StructNew()>     <!---define some keys and values---> <cfset user.first_name="Don"> <cfset user.last_name="VanVliet"> <cfset user.street_address="12 Mojo Lane"> <cfset user.city="Los Angeles"> <cfset user.state="CA"> 

After the structure is defined, you can create any name/value pairs you like, as long as you use the structure's name as a scope when you define each variable. In the preceding example, the key names are first_name, last_name, street_address, city, and state.

To display the value of an item, you call it by its structure name and key:

<cfoutput>#user.last_name#</cfoutput>

The preceding snippet displays the value VanVliet.

An alternative way to display the same value is to use associative array syntax, which accepts the name of a structure and the name of key:

<cfoutput>#user[last_name]#</cfoutput>

That may seem like a more complicated way of displaying a simple value, but it's a useful tool when you want to display the complete contents of a structure as part of a loop. For example, to display all of the elements of the user structure defined above, you could use <cfloop> in "collection" mode, like this:

<cfloop collection=#user# item="key">      <cfoutput>           #key#: #StructFind(user,key)#<br>      </cfoutput> </cfloop>

This special kind of loop enables you to quickly "unpack" the contents of a structure without having to refer to each key by its specific name. It displays output like the following:

first_name: Don last_name: VanVliet street_address: 12 Mojo Lane city: Los Angeles state: CA

Note that you don't have to include the value of the key in your output. It's only used here to show you how <cfloop> loops through the collection, one key at a time. Later in this chapter, you learn how to use a similar <cfloop> to unpack the contents of a special structure called flash, which contains the information sent from a Flash MX movie to a ColdFusion MX template.

end sidebar



 < Day Day Up > 



Macromedia Studio MX Bible
Macromedia Studio MX Bible
ISBN: 0764525239
EAN: 2147483647
Year: 2003
Pages: 491

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net