Passing Additional Var On Service Calls

 

Hey Guys,

If you want to preserve data which is request specifc, I have an example for you:

 

Lets say your using pureMVC and you have a user window, and you make a request by notification and you want to remember who made that call (like the mediator name or something).

 

One way is to add the data to the asyncToken, so

 

var token:AsyncToken = service.getUsers;

toke.preservedData = {bla:bla}

 

And after the result comes,  you extract it from the ResultEvent like:

trace( event.token.preservedData );

 

~~~~~~~~~~~~~~~~

 

There is another way...

 

If you want to keep things more strictly typed, you can you a custom Responder:

 

So on the proxy, when you make the service call, you create a new instanse of a custom Responder, which holds additional parameters. and the code will look like this:

 

=============

 

 UserProxy.as

=============

 

public class UserProxy extends Proxy {

 

public function getUsers(preservedData:Object):void{

 

          // 'service' is some remoteObject you define

 

var token:AsyncToken = service.getUsers();

          token.addResponder(new CustomResponder(handleUsersRecieved, handleServiceFault, preservedData));

}


private function handleUsersRecieved(event : ResultEvent, preservedData : Object):void {


            data = new UserVO(event.result.user);
            sendDataResponse(data,note);
}
       
private function handleServiceFault(event : FaultEvent, preservedData : Object):void{
       // Handle Fault
}

 

]

 

 

=============

 

CustomResponder.as

=============

 

public class CustomResponder implements IResponder {
       
        private var _resultFunction:Function;
        private var _faultFunction:Function;
        private var _preservedData:Object;
       
        public function CustomResponder(resultFunction : Function, faultFunction : Function, preservedData : Object) {
            _resultFunction = resultFunction;
            _faultFunction = faultFunction;
            _preservedData= preservedData;
        }

        public function result(event:Object):void {
            _resultFunction.call(null, event, _preservedData);
        }
       
        public function fault(event:Object):void    {
            _faultFunction.call(null, event, _preservedData);
        }
       
    }

 

Developer