views:

558

answers:

2

I need to return the value from my Responder object. Right now, I have:

private function pro():int {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var id:int = 0;
    function ret_pr(result:*):int {
       return result
    }
    var responder:Responder = new Responder(ret_pr);
    gateway.call('sx.xj', responder);
    return id
}

Basically, I need to know how to get the return value of ret_pr into id or anything that I return from that function. The responder just seems to eat it. I can't use a public variable somewhere else because this will be running multiple times at once, so I need local scope.

+1  A: 

This is how I'd write a connection to the AMF server, call it and store the resulting value. Remember that the result won't be available instantly so you'll set up the responder to "respond" to the data once it returns from the server.

public function init():void
{
    connection = new NetConnection();
    connection.connect('http://10.0.2.2:5000/gateway');
    setSessionID( 1 );
}
public function setSessionID(user_id:String):void
{
    var amfResponder:Responder = new Responder(setSessionIDResult, onFault);
    connection.call("ServerService.setSessionID", amfResponder , user_id);
}

private function setSessionIDResult(result:Object):void {
    id = result.id;
    // here you'd do something to notify that the data has been downloaded. I'll usually 
    // use a custom Event class that just notifies that the data is ready,but I'd store 
    // it here in the class with the AMF call to keep all my data in one place.
}
private function onFault(fault:Object):void {
    trace("AMFPHP error: "+fault);
}

I hope that can point you in the right direction.

Jensa
A: 
private function pro():int {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var id:int = 0;
    function ret_pr(result:*):int {
       return result
    }
    var responder:Responder = new Responder(ret_pr);
    gateway.call('sx.xj', responder);
    return id
}

This code is never going to get you what you want. You need to use a proper result function. The anonymous function responder return value will not be used by the surrounding function. It will always return 0 in this case. You are dealing with an asynchronous call here, and your logic needs to handle that accordingly.

private function pro():void {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var responder:Responder = new Responder(handleResponse);
    gateway.call('sx.xj', responder);
} 

private function handleResponse(result:*):void
{
    var event:MyCustomNotificationEvent = new MyCustomNotificationEvent( 
            MyCustomNotificationEvent.RESULTS_RECEIVED, result);
    dispatchEvent(event);
    //a listener responds to this and does work on your result
    //or maybe here you add the result to an array, or some other 
    //mechanism
}

The point there being using anon functions/closures isn't going to give you some sort of pseudo-syncronous behavior.

Joel Hooks