Flash Remoting (Object Passing) (Page 1 of 3)

Using ColdFusion (or Java or .NET for that matter) you can pass data objects to FlashMX via Flash Remoting. XML objects, arrays, structures, queries, and the like are all fair game. Getting all these types of objects into FlashMX means we can create data-heavy Flash applications that not only look great, but offer a lot of functionality. But how do you do it?

This article will explain how to pass objects from a ColdFusion component to a Flash movie. Before we get started, let me tell you what I am assuming you have experience with. You should know how to get started with Flash Remoting by writing the necessary code to set up a gateway connection. You should know the basics of creating ColdFusion components and how to consume them in Flash applications. This tutorial will not cover these topics and will instead jump write into the code showing you how to create a structure of queries and pass it to a FlashMX application.

The first thing we need to do is create our ColdFusion function. Before you look at the function, it'll make more sense if I explain what it is going to do. I have a FlashMX application called bikeManager v1.0. This application allows me to manage any kind of maintenance I perform on my sportbike. The application supports N number of bikes, allows me to enter maintenance logs to an Access database, and allows me to view the various logs. It's really very handy. The function below was added to my bikeManager CFC and does three things. It queries the database and retrieves all the motorcycles. It queries the database again and retrieves the entire oil change history of one motorcycle. Finally, the function creates a structure of queries that is made up of the two query result-sets. View the example code below and then we'll walk through it.

<cffunction name="GetStructureOfQueries" access="remote" returntype="struct">
    <!--- Flash will not pass any parameters to this method, therefore just pull all the
    sportbikes and oil changes from the DB. --->
    <cfquery name="GetBikes" datasource="##request.datasource##">
	    SELECT BikeID, Make, Model, Year, PurchaseDate
	    FROM Bikes
	    ORDER BY BikeID
    <!--- Since this is just a test Flash will not pass the BikeID needed to get the
    Oil Change History.  Therefore, we'll preset this ourselves. --->
    <cfset BikeID = "1">
    <cfquery name="GetOilChangeHistory" datasource="##request.datasource##">
	    SELECT Bikes.BikeID, Bikes.Make, Bikes.Model, OilChanges.BikeID,
	           OilChanges.OilUsed, OilChanges.OilWeight, OilChanges.FilterChanged,
	           OilChanges.FilterUsed, OilChanges.OilMileage, OilChanges.OilDate,
	    FROM Bikes, OilChanges
	    WHERE Bikes.BikeID = ##BikeID## AND OilChanges.BikeID = Bikes.BikeID
	    ORDER BY OilChanges.OilDate DESC
    <!--- Create the structure that will hold these two queries. --->
    <cfset returnStruct = StructNew()>
    <cfset returnStruct.GetBikes = GetBikes>
    <cfset returnStruct.GetOilChangeHistory = GetOilChangeHistory>
    <cfreturn returnStruct>