I asked on CF Talk ( Cf-Flash) for some help on remoting, and Tyron Foston came up with a great solution


How To Use the AS3 FlashRemoting Class



1) Create an instance of the FlashRemoting class

   -The constructor takes the location of the flash

   remoting servcie as its only parameter



2) Set a "onLoad" handler to handle the results coming

   from the server



3) Use the "load" function to send a flash remtoing call

   to the coldfusion server. This takes 2 parameters

   a) the cfc and function you want to call

   b) parameter. This only takes 1 parameter. If you want

      to pass multiple variables, place then into an object

      and pass the object



4) There is a function called "toQuery" that takes a recordset

   from coldfusion and turns it into an object with index arrays







Working Example

---------------------------------------------------------------

//Create an indexed array with values,

//this will be the parameters passed to the coldfusion server

var array = new Array()

array[0] = 'car'

array[1] = 'boat'

array[2] = 'ship'



//Create new instance of the FlashRemoting Class

var fr:FlashRemoting =

new FlashRemoting('http://www.mydomain.com/flashservices/gateway')



//Set the onLoad handler for when the flash gets the server response

fr.onLoad = function(obj){



	//I'm expecting a query, so use the "toQuery" function

	var myQuery = this.toQuery(obj)



}



//Call the method on the remoting cfc (relative to the gateway url)

fr.load('nameOfCFC.nameOfFunction', array)




package{



	//Import needed classes

	import flash.events.*

	import flash.net.NetConnection

   	import flash.net.ObjectEncoding;

    import flash.net.Responder;

	import flash.display.MovieClip;



	//Create new public class

	public dynamic class FlashRemoting{



		//Set Class variables



		//Set class variable for the netConnection

		private var nc:NetConnection





		/*

		Create the constructor function taking the remote server URL

		as the only function parameter

		*/

		public function FlashRemoting(url:String){



				//Create netConnection object and event listeners

				this.nc = new NetConnection()



				//Set the AMF encoding method

				this.nc.objectEncoding = ObjectEncoding.AMF0



				//Set the netConnection client property to this class to handle all

				this.nc.client = this



				//Make the connection to the remote server

				this.nc.connect(url)



				//Set the event listener to listen for net_status events

				this.nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus)



		}





		/*

		The function to be invoked when there is a connection problem.

		The event information is passed to the onLoad function

		*/

		private function onNetStatus(event){

			//trace("Event:"+event)

			var obj:Object = event.info

			//trace(""+event)

			for(var i in event){

				//trace(i+":"+event[i])

			}

			this.onLoad(obj)

		}







		/*

		Makes a call to the remote server. The first parameter

		is the location.method to call on the cfc

		The second paramter is an array of parameter for the

		remote method to consume

		*/

		public function load(method:String, args){

			//Call the method on the server

			var responder = new Responder(this.getResult, this.getError);

			this.nc.call(method, responder, args)

		}







		/*

		The function that gets called if everything goes right

		This function passes the result from the server, to

		the onLoad function

		*/

		private function getResult(obj){

				try{

					//trace("getResult")

					this.onLoad(obj)

				}

				catch(e){

					//Do Nothing

				}

		}



		/* The function that gets called with things go

		wrong. The String 'ERROR' is always passed to the

		onLoad function

		*/

		private function getError(obj){

				try{

					this.onError(obj)

				}

				catch(e){

					//Do Nothing

				}

		}





		public function toQuery(obj:Object):Object{





			//trace(serverData.columnNames)

			//trace("Version:"+serverData.version)

			//trace("Cursor:"+serverData.cursor)

			//trace("Total Count:"+serverData.totalCount)

			//trace("Inital Data:"+serverData.initialData[0][1])



			var serverData = obj['serverInfo']

			var columns:String = serverData.columnNames

			var columnArray = columns.split(',')

			var columnObj = new Object()

			var query = new Object()

			var totalRows:Number = serverData.totalCount

			var currentRow:Number = 0

			var a:Number



			//Create tmp columns arrat and begin query structure

			for(a=0; a<columnArray.length; a++){

				columnObj[columnArray[a]] = a

				query[columnArray[a]] = new Array()

			}





			//Loop through the initalData property (query data)

			for(a = 0; a < columnArray.length; a++){

				for(currentRow = 0; currentRow < totalRows; currentRow++){

					query[columnArray[a]][currentRow] = serverData.initialData[currentRow][a]

				}

			}



			//Return the query object

			return query

		}





	}//end class



}//end package



The main flash file


//Create an indexed array with values, this will be the parameters passed to the coldfusion server



var array = new Array()

array[0] = 'car'

array[1] = 'boat'

array[2] = 'ship'



//Create new instance of the FlashRemoting Class

var fr:FlashRemoting = new FlashRemoting('http://localhost/flashservices/gateway')







//Set the onLoad handler for when the flash gets the server response



fr.onLoad = function(obj){

//I'm expecting a query, so use the "toQuery" function

	var myQuery = this.toQuery(obj)

}

//Call the method on the remoting cfc (relative to the gateway url)

fr.load('com.cfcs.testing.getEmails', array)

trace(myQuery);





The cfc


<cfcomponent>

	<cffunction name="getResults" access="remote" returntype="query">

	  <cfargument name="getEmails" type="string" required="yes">

	    <cfquery name="getEmails" datasource="johnbarrett">

            SELECT *

            FROM testing

        </cfquery>

	    <cfreturn getEmails>

    </cffunction>

</cfcomponent>



I get an “undefined error” ūüôĀ

By |2009-04-08T02:27:37+00:00April 8th, 2009|