Actionscript 3 试图理解Flex/Actionscript中的AsyncToken

Actionscript 3 试图理解Flex/Actionscript中的AsyncToken,actionscript-3,flex3,asynchronous,Actionscript 3,Flex3,Asynchronous,我试图理解AsyncToken在actionscript中的工作方式。如何调用远程服务并确保结果或故障事件函数中有特定参数可用?我认为这是我想要使用的异步功能 下面的代码有望解释我正在尝试做什么。请随意修改代码块作为您的解释 谢谢 public function testSerivceCall(data:Object, callBackCommand:String):void { // Assume callBackCommand == "FOO"; // How can I p

我试图理解AsyncToken在actionscript中的工作方式。如何调用远程服务并确保结果或故障事件函数中有特定参数可用?我认为这是我想要使用的异步功能

下面的代码有望解释我正在尝试做什么。请随意修改代码块作为您的解释

谢谢

public function testSerivceCall(data:Object, callBackCommand:String):void
{
    // Assume callBackCommand == "FOO";
    // How can I pass in callBackCommand as a parameter to the result or fault events?
    // How do I create an async token here?

    var remoteObject:RemoteObject;
    remoteObject = new RemoteObject();
    remoteObject.destination = "zend";
    remoteObject.source = "MyService";
    remoteObject.endpoint = "http://example.com/service";
    remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult);
    remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault);
    remoteObject.test(data);
}

private function _handleTestResult( event:ResultEvent ) : void 
{ 
    // How do I get the async token value?
    // How can I get the value of callBackCommand in this code block?

    if (callBackCommand == "FOO")
    { 
        // do something related to "FOO"
    }
    else
    {
        // do something else with the result event
    }


} 

private function _handleTestFault( event:FaultEvent ) : void 
{ 
    // How do I get the async token value?
    // How can I get the value of callBackCommand in this code block?
} 
编辑以使此问题更清楚:

假设我在代码中的某个地方调用了以下方法:

testSerivceCall(personObject, "LoginCommand");
如何访问handleTestResult函数块中的实际字符串“LoginCommand”

我之所以要这样做,是因为我想动态地回调某些函数,并将结果数据传递给我在进行服务调用时提前知道的特定命令


我正在摸索AsyncToken的语法和功能。

如果我正确地阅读了您的问题,您是在试图找出如何访问
结果事件返回的实际数据

如果是这样,假设您正确拨打了电话,并且以您期望的格式返回了数据:

private function _handleTestResult( event:ResultEvent ) : void 
{ 
  // you get the result from the result property on the event object
  // edit: assuming the class Person exists with a property called name
  //       which has the value "John"
  var person : Person = event.result as Person;

  if (person.name == "John")
  { 
      Alert.show("John: " + person.name);
  }
  else
  {
      Alert.show("Not John: " + person.name);
  }
} 

private function _handleTestFault( event:FaultEvent ) : void 
{ 
  // Maybe you know the type of the returned fault
  var expectedFault : Object = event.fault as MyPredefinedType

  if (expectedFault.myPredefinedTypesPredefinedMethod() == "BAR")
  {
    // something here
  }
}
ResultEvent有一个名为result的属性,它将保存结果返回的对象的实例(例如,如果使用web服务,它可能是XML文件的输出,如果使用AMF,它可能是序列化对象)。这是您想要访问的内容。类似地,FaultEvent具有返回故障信息的fault属性


编辑:响应Gordon Potter的评论,更改了
\u handleTestResult()
中的代码。

如果要访问远程调用期间使用的属性(调用和/或AsycToken的参数),可以使用闭包。只需将调用方法中的结果事件处理程序定义为闭包。然后,它可以访问调用函数中的任何变量

public function testSerivceCall(data:Object, callBackCommand:String):void
{
    var _handleTestResult:Function = function( event:ResultEvent ) : void 
    { 
        // token is visible here now
        if (callBackCommand == "FOO")
        { 
            // do something related to "FOO"
        }
        else
        {
            // do something else with the result event
        }
    }

    var remoteObject:RemoteObject;
    remoteObject = new RemoteObject();
    remoteObject.destination = "zend";
    remoteObject.source = "MyService";
    remoteObject.endpoint = "http://example.com/service";
    remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult);
    remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault);
    var token = remoteObject.test(data);
}

我甚至不需要闭包。我添加了一个类,如下所示,我从外部调用了该类

电话是这样的:

 public class MyClass
 {
    ...
    var adminServerRO:AdminServerRO = new AdminServerRO();
    adminServerRO.testSerivceCall("FOO",cptyId);
 }

public class AdminServerRO
{

   private function extResult( event:ResultEvent, token:Object ) : void
   {
       //the token is now accessed from the paremeter
        var tmp:String = "in here";
   }

   private function extFault( event:FaultEvent ) : void
   {
     var tmp:String = "in here";
   }


   public function testSerivceCall(callBackCommand:String, cptyId:String):void 
   { 
      var remoteObject:RemoteObject = new RemoteObject(); 
      remoteObject.destination = "adminServer"; 
      var token:AsyncToken = remoteObject.getCounterpartyLimitMonitorItemNode(cptyId);
      token.addResponder(new AsyncResponder(extResult,extFault,cptyId));
   } 

}

虽然接受的答案将满足原始提交人的要求,但实际上并没有回答所提出的问题。AsyncToken是作为远程方法调用的结果创建的,可以从ResultEvent访问。因为AsyncToken是一个动态类,所以您可以向它添加您想要的任何属性。以下代码应说明这一点:

public function testSerivceCall(data:Object, callBackCommand:String):void
{

     var remoteObject:RemoteObject;
     remoteObject = new RemoteObject();
     remoteObject.destination = "zend";
     remoteObject.source = "MyService";
     remoteObject.endpoint = "http://example.com/service";
     remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult);
     remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault);
     var token:AsyncToken = remoteObject.test(data);
     token.callBackCommand = callBackCommand;
}

private function _handleTestResult( event:ResultEvent ) : void 
{ 

     if (event.token.callBackCommand == "FOO")
     { 
         // do something related to "FOO"
     }
     else
     {
         // do something else with the result event
     }


 } 

 private function _handleTestFault( event:FaultEvent ) : void 
 { 
      //event.token.callBackCommand should be populated here too
 }

好的,我认为这部分是有道理的。但我想我需要更清楚一点。假设上面的代码,然后假设我在项目中的某个地方进行了以下函数调用:testserivecall(personObject,“John”);现在在我的服务结果中,如何访问实际字符串“John”?如果是一个包含“John”的person对象,也许我想做一些非常具体的事情。这有意义吗?好的,这有意义,谢谢!仍然在处理闭包。