Posted: April 15, 2010 at 9:42 AM by jbuda
Tags: Actionscript, Coldfusion
The following code snippets are Actionscript 3 classes that have enabled me to connect to Coldfusion components running on the server to interact with the database. The classes are pretty simple and only require a couple of arguments passed to them to begin working with the gateway.
The first snippet is a custom event that has been created so when the data has been returned from the server it can dispatch the event to notify the application.
RemotingEvent.as
package {
import flash.events.Event;
public class RemotingEvent extends Event {
public static const RESULT:String = 'onResult';
public var params:Object;
public function RemotingEvent($type:String, $params:Object, $bubbles:Boolean=false, $cancelable:Boolean=false) {
super($type, $bubbles, $cancelable);
this.params = $params;
}
public override function clone():Event {
return new RemotingEvent(type, bubbles, cancelable, params);
}
}
}
The following class is where we connect to the gateway and call the functions from the cfc to run the desired code on the Coldfusion component. The init function is called from the application with the name of method to run on the component, any arguments that are required are also sent through the gateway to Coldfusion.
RemotingService.as
package {
import flash.net.NetConnection;
import flash.net.Responder;
import RemotingEvent;
public class RemotingService extends NetConnection {
private var _service:NetConnection;
private var _Url:String;
private var _cfc:String;
private var _responder:Responder;
function RemotingService($url,$cfc) {
_Url = 'http://'+$url+'/flashservices/gateway';
_cfc = $cfc;
_service = new NetConnection();
_service.connect(_Url);
_responder = new Responder(onResult, onFault);
}
public function init($function:String,$vars:String=''):void {
_service.call(_cfc+'.'+$function,_responder,$vars);
}
private function onResult(e:Object):void {
dispatchEvent(new RemotingEvent(RemotingEvent.RESULT,e));
}
private function onFault(fault:Object):void {
trace('REPORTING AN ERROR');
trace('==============');
for (var str:String in fault) {
trace(str+' : '+fault[str]);
}
trace('==============');
for (var str2:String in fault['rootcause']) {
trace(str2+' : '+fault['rootcause'][str2]);
}
}
}
}
The classes above are utilised with the following snippet, that i would have used from the application after it has initialised to get the data from the server that is required.
var _remoting:RemotingService = new RemotingService('http://www.yourdomain.com','com.remoting.RemotingService');
_remoting.addEventListener(RemotingEvent.RESULT,DataReturn);
_remoting.init('FUNCTION_ON_CFC','ANY ARGUMENTS');
When data has been returned from the server, the function 'DataReturn' is called with the arguments to that function with the results from the server. This can then be used as required in your application.
It is a simple setup, but one that i have found quite useful and allows for reuse across most of the Flash Remoting / Coldfusion applications that i have needed to create.

May 18, 2011 at 8:19 AM Excellent code. Thank you for taking the time to share it! It works like a charm:) I am having an issue that I never had in AS2. I do not seem to be able to parse the recordset. I know that I can have coldfusion return arrays instead, but in an effort to avoid redoing all that cf I would like to be able to parse the recordset in flash. Any thoughts? thank you again!! John
May 18, 2011 at 8:46 AM @john are you having any problems getting the data into Flash? How is the data being passed into Flash?
May 18, 2011 at 10:13 AM Hey jbuda, I am actually getting the recordset back from coldfusion just fine now but am losing the Column names. I am parsing things this way: function DataReturn_current($return) { var cf_return:Array = $return.params.serverInfo.initialData; for (var i in cf_return) { var thisItem = cf_return[i]; thisItem._id = thisItem[0]; thisItem._name = thisItem[1]; thisItem._status = thisItem[2]; //etc... allItems_array.push(thisItem); } } As you can see though it is not very graceful. I would much prefer to pull the column names with the remote call and have it look like this in the return function DataReturn_preferred($return) { var cf_return:Array = $return.params.serverInfo.arrayOfSomeSort; for (var i in cf_return) { var thisItem = cf_return[i]; thisItem._id = thisItem.ID thisItem.Name = thisItem.NAME; thisItem.Status = thisItem.STATUS; //etc... allItems_array.push(thisItem); } } Any ideas? Thank you again! John