从Flex / ActionScript 3 Responder对象返回

时间:2009-11-12 19:19:30

标签: flex actionscript-3 amf netconnection

我需要从Responder对象返回值。现在,我有:

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
}

基本上,我需要知道如何将ret_pr的返回值转换为id或从该函数返回的任何内容。响应者似乎只是吃了它。我不能在其他地方使用公共变量,因为它会一次运行多次,所以我需要本地范围。

2 个答案:

答案 0 :(得分:2)

这是我写AMF服务器的连接,调用它并存储结果值的方法。请记住,结果将无法立即生效,因此您将设置响应程序,以便在从服务器返回后“响应”数据。

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);
}

我希望能指出你正确的方向。

答案 1 :(得分:0)

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
}

这段代码永远不会让你得到你想要的东西。您需要使用正确的结果函数。周围的函数不会使用匿名函数响应器返回值。在这种情况下,它总是返回0。你正在处理异步调用,你的逻辑需要相应地处理它。

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
}

使用匿名函数/闭包这一点不会给你某种伪同步行为。