5.2 How ColdFusion Fits into Flash Applications

I like to think of remote services (services provided by a computer other than the computer the client is running on) as extensions of the client. In fact, once you get used to incorporating a server-side aspect to your Flash applications?and especially once you have a library of reusable services in place?it is difficult to think of application development without integrating server-side functionality.

Writing services in ColdFusion provides some nice options to Flash application developers. ColdFusion is simple enough to allow the Flash developer to also write the remote services that his application needs. Alternatively, projects can be organized to create more of a division between remote services and the client code that uses them. One team of developers can provide various remote services, such as database interaction or email capability, while another team builds the front end of the application that makes use of those services.

Regardless of who on the team does the work, you should divide your application or set of services into presentation logic and business logic. Business logic is the rules and workflow that model your enterprise. Code that handles account creation or credit card transactions is an example of business logic. Presentation logic is the portion of your application that presents data to the user and allows for interactivity. Code that allows users to drag items into a shopping cart or display error messages is an example of presentation logic. Flash is an excellent choice for developing and deploying sophisticated presentation logic that allows for a high degree of interaction and contains rich, compelling content. ColdFusion MX is a fitting choice for business logic implementation because of its ease-of-use, versatility, and seamless integration with Flash.

To successfully implement an application or service using two different technologies, we need an efficient way for the two technologies to communicate, which in the programming world usually means passing objects and other forms of data back and forth. The basis for communication between Flash and ColdFusion is, of course, Flash Remoting. Figure 5-1 demonstrates the relationship between Flash and ColdFusion in the context of an integrated application.

Figure 5-1. The relationship between Flash, ColdFusion, and Flash Remoting
figs/frdg_0501.gif

5.2.1 Datatype Conversions

Before we get into examples of how data can be passed back and forth between Flash and ColdFusion, let's take some time to examine how datatypes are handled between client-side ActionScript code and server-side ColdFusion. In other words, as objects and primitive datatypes are passed from client to server and vice versa, how do those datatypes change?

5.2.1.1 Datatypes in Flash

Flash supports both primitive datatypes and reference datatypes (also called composite or complex datatypes). Primitive datatypes, such as string, number, and boolean have distinctive characteristics:

  • They contain a single data value.

  • Primitive datatypes are passed by value, meaning that when they are passed as parameters to a function, a copy of the value of the data is passed, not a reference to the data. The value of a primitive datatype in a calling routine cannot be changed from within the called routine.

  • Making a copy of a primitive datatype and then changing its value does not change the value of the original variable.

Reference datatypes, such as Object, Array, RecordSet, and MovieClip, have the following characteristics:

  • They do not contain data themselves; rather, they contain references to objects that usually contain data and functions. They point to a place in memory where the data of interest resides.

  • Attributes or elements of a reference datatype can change without changing the fact that a variable can refer to the object's or array's container. For example, you might have a reference to an object with a certain set of properties, and those properties are free to change while you maintain a reference to that object. Likewise, adding or deleting elements from an array doesn't change the fact that a variable might refer to the array.

  • Making a copy of a reference datatype generally creates another reference to the datatype in memory (similar to the way that creating a Windows shortcut or Macintosh alias doesn't create a new filename but rather points to an existing file).

  • Reference datatypes are generally passed by reference, meaning that when they are passed as parameters to a function, changes made to the data within the called routine may affect the data in the calling routine as well.

The null and undefined datatypes are used to indicate the absence of a value or reference.

5.2.1.2 Datatypes in ColdFusion

Although ColdFusion uses different terminology, ColdFusion datatypes are very similar to ActionScript datatypes. ColdFusion uses the term simple (as opposed to primitive) to describe datatypes that can contain a single value, such as numbers, strings, and booleans.

Since ColdFusion does not support objects to the extent that ActionScript does, it does not support an exact equivalent of Flash's reference datatypes. However, ColdFusion does have a collection of complex datatypes (a.k.a. data containers or data structures), such as arrays, queries, and structures, that contain data that can change.

ColdFusion also supports a binary datatype, which handles the contents of things like image files or MP3s, and an external object datatype that is used for things like Java objects. These two datatypes cannot be returned to a Flash application.

5.2.1.3 Passing data between Flash and ColdFusion

Even though datatypes are similar between Flash and ColdFusion, some differences must be accounted for when passing data back and forth. Table 5-1 shows datatype conversions between Flash and ColdFusion.

Table 5-1. Datatype conversions between ActionScript and ColdFusion

Flash (ActionScript)

ColdFusion

ActionScript object

Struct (or ASObject)

Array

Array

Associative array

Struct

Boolean

Boolean

Date

Date

Number

Number

RecordSet

Query object

String

String

Undefined

Null

XML

XML document

Null

Null

As you can see, there is a close correlation between ActionScript and ColdFusion datatypes. This helps to make integration of the two technologies virtually seamless. The Flash Remoting gateway does the work of converting the objects from ColdFusion into ActionScript and back again.

5.2.2 Flash Variable Scope

ColdFusion supports the Flash variable scope, which is used primarily when ColdFusion pages are accessed as remote services. The Flash scope is used like other variable scopes in ColdFusion, such as URL, Session, and Request. For example, Flash.Result is the Result variable of the Flash scope.

For ColdFusion pages, the Flash scope is the only way to pass data to and from the service. Although the Flash scope can also be used with services built as CFCs, the recommended approach is to receive arguments using the <cfargument> tag and return data using the <cfreturn> tag.

There are three built-in variables in the Flash scope to pass data to and from the Flash movie:

Flash.Params

An array of parameters passed from the Flash movie

Flash.Result

A return object that is passed back to the Flash movie from the ColdFusion page

Flash.Pagesize

The number of records to return from a <cfquery> to the Flash movie

The Params variable is an array of arguments passed to your remote service call. They have to be accessed in your ColdFusion page by number, starting with 1 as ColdFusion arrays do. For example, if you call a method from your client-side ActionScript:

var first="Tom";
var last="Muck";
var email="tom@tom-muck.com";
myService.saveEmployeeRecord(first, last, email);

you can access the arguments on the ColdFusion page like this:

<cfset first = Flash.Params[1]>
<cfset last = Flash.Params[2]>
<cfset email = Flash.Params[3]>

You can also use the Flash variable scope to access named elements of an ActionScript object. The shorthand way of creating an object in Flash is to use an object literal, where each name/value pair has the syntax label:value:

var first="Tom";
var last="Muck";
var email="tom@tom-muck.com";
myService.saveEmployeeRecord({first:first, last:last, email:email});

Again, you could access the variables in ColdFusion using the Flash scope:

<cfset first = Flash.first>
<cfset last = Flash.last>
<cfset email = Flash.email>

The Flash.Result variable is used to return results from the service to the Flash movie. In a ColdFusion page, unlike the CFC services that we built in earlier examples, Flash.Result is the only way to return a parameter to the Flash movie. This variable can contain any type of result, such as a struct, recordset, string, or Boolean. Simply setting the Flash.Result variable triggers the return of the parameter to the Flash movie.



    Part III: Advanced Flash Remoting