【问题标题】:Returning from Flex/ActionScript 3 Responder objects从 Flex/ActionScript 3 Responder 对象返回
【发布时间】:2009-11-12 19:19:30
【问题描述】:

我需要从我的 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 或我从该函数返回的任何内容。响应者似乎只是吃了它。我不能在其他地方使用公共变量,因为它会一次运行多次,所以我需要本地范围。

【问题讨论】:

    标签: apache-flex actionscript-3 amf netconnection


    【解决方案1】:

    这就是我编写与 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);
    }
    

    我希望这可以为您指明正确的方向。

    【讨论】:

      【解决方案2】:
      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
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-06
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多