Creating a Remote Object DataManager in ActionScript 3.0 for Flex 2.0

Tien Nguyen followed up on my WebService DataManager with one for RemoteObject as well.
I haven’t had a chance to try it myself yet, but this RemoteObject DataManager should work through the CFAdapter for more efficient data transport with ColdFusion Components

package managers {
    import flash.events.EventDispatcher;
    import flash.util.*;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.AbstractOperation;
    import mx.rpc.remoting.mxml.RemoteObject;
    import mx.utils.ObjectUtil;
    import events.DataManagerResultEvent;
    import mx.controls.Alert;
 
  /**
   DataManager - singleton class which enforces only
   a single object is created for each service.  To
   access DataManager, use getDataManager(cfservice:String)
  */
 
    public class DataManagerCF extends EventDispatcher {
        private var CFsvc:RemoteObject;
        private var eventName:String;
 
        // hashmap of instances for each service
        private static var instanceMap:Object = new Object();
 
        public function DataManagerCF(pri:PrivateClass,dest:String){
            this.CFsvc = new RemoteObject();
            CFsvc.destination = dest;
        }
 
        public static function getDataManager(CFsvc:String):DataManagerCF{
          if(DataManagerCF.instanceMap[CFsvc] == null){
               DataManagerCF.instanceMap[CFsvc] = new DataManagerCF(new PrivateClass(),CFsvc);
          }
          var dm:DataManagerCF= DataManagerCF.instanceMap[CFsvc];
          return dm;
        }
 
        public function makeRemoteCall(methodName:String,eventName:String,...args:Array):Void{
            this.eventName = eventName;
            var op:mx.rpc.AbstractOperation = CFsvc[methodName];
            CFsvc.addEventListener("result", doResults);
            CFsvc.addEventListener("fault", doFault);
            if(args.length >0){
                 op.send.apply(null,args);
            }  else {
                 op.send();
            }
        }
 
        private function doResults(result:ResultEvent):Void{
             var e:DataManagerResultEvent = new DataManagerResultEvent(eventName,result.result);
             this.dispatchEvent(e);
        }
 
        private function doFault(fault:FaultEvent){
             this.dispatchEvent(fault);
        }
 
        public override function toString():String{
             return "DataManagerCF";
        }
    }
 
  /**
   PrivateClass is used to make
   DataManager constructor private
  */
 
  private class PrivateClass{
        public function PrivateClass() {}
  }
}

 

I’ll post more on this in the next few days as I get a chance to work with it.

2 Comments

    Any more news on this, possibly a few examples of how to use it?

    • by A visitor
    • 12:10 am, October 31, 2006
    • Reply

    The styling for the code sample area is horrible, I had to copy and read somewhere else, the white on gray is just unreadable.

    • by A visitor
    • 12:06 am, June 27, 2007
    • Reply

Leave a Reply