Wednesday, June 1, 2011

Wrapper class for RemoteObject

In my projects I use AMF connections very often. In my of my previous posts I showed you PHP class and some Actionscript code that connects to it (LINK). This code is fine when you have very simple application, but in bigger projects it's very handy to write a wrapper class for handling AMF connections. Let's start with writing AMFConnection Class:

public dynamic class AMFConnection extends RemoteObject
{
 protected var _resultCallback:Function = null;
 protected var _faultCallback:Function = null;
 
 public function AMFConnection(destination:String, endpoint:String, source:String, resultCallback:Function = null, faultCallback:Function = null)
 {
  super(destination);
  super.endpoint = endpoint;
  super.source = source;
  showBusyCursor = true;
  
  if (resultCallback != null)
  {
   _resultCallback = resultCallback;
  }
  
  if (faultCallback != null)
  {
   _faultCallback = faultCallback;
  }
  
  addEventListener(ResultEvent.RESULT, serverResultHandler);
  addEventListener(FaultEvent.FAULT, serverFaultHandler);
 }
 
 protected function serverResultHandler(event:ResultEvent):void
 { 
  var returnData:Object = event.result;
  
  if (_resultCallback != null)
  {
   _resultCallback(returnData);
  }
 }
 
 protected function serverFaultHandler(event:FaultEvent):void
 {
  Alert.show("\n" + event.fault.faultString + "\n\n", "AMF Connection error");
  
  if (_faultCallback != null)
  {
   _faultCallback();
  }
 }
}

As you can see my class extends RemoteObject class. It's very important to make it dynamic - without it it would be impossible to call functions that is not explicitly defined in this class. You can use it straight away in your code but I usually make more classes that derives from this one. I write one Actionscript class for each PHP class on the server. It is nice idea to name them identically:

public dynamic class MyService extends AMFConnection
{
 public function MyService(resultCallback:Function = null, faultCallback:Function = null)
 {
  super("amfphp", "http://some.server.address/gateway.php", "MyService", resultCallback, faultCallback);
 }
}

Now when you instantiate new MyService class in your code you have RemoteObject that is already set. When there is any change (for example new server address) you just have to change it only in one place in your Actionscript code. Additionally during instantiation there is also an option to specify result anf fault callbacks:

var myService:MyService = new MyService(myResultCallback, myFaultCallback);
myService.someServiceFunction();

Result callback should have one parameter of ArrayCollection type:

protected function myResultCallback(data:ArrayCollection):void
{ 
 for each (var item:Object in data)
 {
  trace(item);
 }
}

Fault callback doesn't have any parameters. This function is usually not used because in AMFConnection class there is already code that shows a pop up window with error information. Anyway, it could be useful to put some code there in specific situations:

protected function myFaultCallback():void
{ 
 trace("MyService Failed :-(");
}

That's all - I hope this code would be useful to you. If you have better idea how to write a wrapper class for AMF connections please let me know.

No comments:

Post a Comment