2 Replies Latest reply on Nov 9, 2011 3:11 AM by Francisc

    How can I listen for events from an AsyncToken var outside the implementing class

    Francisc Level 3

      Hello,

       

      I'm using this:

      public function doSomething():void

      {

           var token:AsyncToken=myRemoteObject.getData();

           token.addResponder(new mx.rpc.Responder(resultHandler,faultHandler));

      }

      private function resultHandler(event:ResultEvent):void

      {

           dispatchEvent(event);

      }

      private function faultHandler(event:FaultEvent):void

      {

           dispatchEvent(event);

      }

       

      And listening for the events on the instantiated var.

       

      Is that the proper way to do this, or is there a better one?

       

      Thank you.

        • 1. Re: How can I listen for events from an AsyncToken var outside the implementing class
          drkstr_1 Level 4

          I usually see service classes constructed similar to this:

           

          public interface IMyService
          {
                    function loadData(id:int):AsyncToken;
                    function publishData(data:Object):AsyncToken;
          }
          
          public class MyService implements IMyService
          {
                    private var _remoteObject:RemoteObject;
          
                    protected function get remoteObject():RemoteObject
                    {
                              if(!_remoteObject)
                              {
                                        _remoteObject = new RemoteObject("MyDestination");
                                        _remoteObject.showBusyCursor = true;
                                        _remoteObject.source = "my_service";
                              }
          
          
                              return _remoteObject;
                    }
          
                    public function loadData(id:int):AsyncToken
                    {
                              return this.remoteObject.loadData(id);
                    }
          
                    public function publishData(data:Onject):AsyncToken
                    {
                              return this.remoteObject.publishData(data);
                    }
          
          }
          
          public class MyController
          {
          
                    public var service:IMyService = new MyService(); //or find a better way to inject
          
                    public function doStuff(id:int):void
                    {
                              var token:AsyncToken = this.service.loadData(id);
                              token.addResponder(new Responder(loadData_resultHandler, loadData_faultHandler));
                    }
          
               ...
          }
          
          

           

           

          The actual implementation may vary, but the idea is to have a class with methods that mirrors your service class methods, and then abstract this using an interface to allow for different service implementations (if desired of course). This is type of class is usually referred to as a delegate or proxy class.

           

           

          I think mostly it's a matter of personal preference, but I have seen it implemented this way by a lot of major frameworks (the Flash Builder service generator is one of them).

           

          Fixed type in code.