Monday, November 19, 2007

ActionScript equivalent of mx:remoteobject


import mx.rpc.remoting.RemoteObject;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.InvokeEvent;
import mx.messaging.Channel;
import mx.messaging.channels.AMFChannel;
import mx.messaging.ChannelSet;
import flash.events.Event;

import mx.controls.Alert;


public function onCreationComplete () : void
{
//create the remote objects first
createRemoteObjects();

//set the AMF Channel on the remote objects
setUpAmfChannel();
}

public function createRemoteObjects () : void
{
ro = new RemoteObject();
ro.destination = "ColdFusion";
ro.source = "path.to.cfc";

ro.addEventListener("fault",ro_fault_handler);

//You probably want and need a result handler...
ro.methodName.addEventListener("result",methodName_result_handler);
//You may need this if you need to do something when a method is called
ro.methodName.addEventListener("invoke",methodName_invoke_handler);
//You may need this if you need to catch and handle errors differently
ro.methodName.addEventListener("fault",methodName_fault_handler);
//This is just sugar, you really dont need it unless you want it.
ro.methodName.showBusyCursor = true;
}


/* You can call the following method anytime to set the amf channel for a remote object on the fly */
public function setUpAmfChannel () : void
{
var amfChannel:Channel = new AMFChannel("my-cfamf","http://server/flex2gateway/");
amfChannelSet = new ChannelSet();
amfChannelSet.addChannel(amfChannel);

//repeat the following line for all remoteObjects
this.ro.channelSet = amfChannelSet;
}

public function ro_fault_handler ( event:FaultEvent ) : void
{
Alert.show( event.fault.faultString, 'Error');
}

public function call_methodName () : void
{
//use this to call the method
ro.methodName(/*pass arguments here*/);
}

public function methodName_invoke_handler ( event:InvokeEvent ) : void
{
//this will fire when the method is invoked
}

public function methodName_fault_handler ( event:FaultEvent ) : void
{
//this will fire when the method throws an error
}

public function methodName_result_handler ( event:ResultEvent ) : void
{
//this will fire when a result comes back from the method
}

No comments: