尝试理解 Flex/Actionscript 中的 AsyncToken

发布于 2024-07-30 07:05:55 字数 1634 浏览 8 评论 0原文

我试图了解 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 语法和功能。

I am trying to understand the way the AsyncToken works in actionscript. How can I call a remote service and ensure that a specific parameter is available in the result or fault event functions? I think it is the async functionality I want to use.

The following code will hopefully explain what I am trying to do. Feel free to modify the code block as your explanation.

Thanks.

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?
} 

An edit to make this question more clear:

Assume I make the following method call somewhere in my code:

testSerivceCall(personObject, "LoginCommand");

How do I get access to the actual string "LoginCommand" inside the _handleTestResult function block?

The reason I want to do this is because I want to dynamically call back certain functions and hand off the result data to specific commands that I know ahead of time when I am making the service call.

I am just having a time grokking the AsyncToken syntax and functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

通知家属抬走 2024-08-06 07:05:55

虽然接受的答案将实现原始提交者想要的结果,但它实际上并没有回答所提出的问题。 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
 }

While the accepted answer will accomplish what the original submitter wants it does not actually answer the question which was asked. An AsyncToken is created as a result of a remote method call and is accessible from the ResultEvent. Since AsyncToken is a dynamic class you can add whatever property to it that you want. The code below should demonstrate this:

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
 }
潦草背影 2024-08-06 07:05:55

我什至不需要关闭。 我添加了一个类,如下所示,我在外部调用它。

通话是这样的:

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

}

I did not even need closures. I added a class as below which I called externally.

The call was like this:

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

}

窗影残 2024-08-06 07:05:55

如果您想访问远程调用期间使用的属性(调用的参数和/或 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);
}

If you want to access the properties used during the remote call (parameters to the call and/or AsycToken), you can make use of closures. Just define the result event handler inside the calling method as a closure. It can then access any variable in the calling function.

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);
}
一个人的旅程 2024-08-06 07:05:55

如果我正确地阅读了您的问题,您正在尝试弄清楚如何访问 ResultEvent 返回的实际数据?

如果是这样,假设您已正确进行调用并且已以您期望的格式返回数据:

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 有一个故障属性,用于返回故障信息。

编辑:更改了 _handleTestResult() 中的代码以响应戈登·波特的评论。

If I'm reading your question correctly, you're trying to figure out how to access the actual data returned by the ResultEvent ?

If so, assuming you've made the call correctly and you've gotten data back in a format you're expecting:

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

The ResultEvent has a property called result which will hold an instance of the object returned by the result (it might be the output of an XML file if using a web service, or a serialized object if using AMF, for example). This is what you want to access. Similarly, FaultEvent has a fault property that returns the fault information.

Edit: Changed code in _handleTestResult() in response to Gordon Potter's comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文