尝试理解 Flex/Actionscript 中的 AsyncToken
我试图了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然接受的答案将实现原始提交者想要的结果,但它实际上并没有回答所提出的问题。 AsyncToken 作为远程方法调用的结果而创建,并且可以从 ResultEvent 访问。 由于 AsyncToken 是一个动态类,您可以向其中添加所需的任何属性。 下面的代码应该演示这一点:
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:
我什至不需要关闭。 我添加了一个类,如下所示,我在外部调用它。
通话是这样的:
}
I did not even need closures. I added a class as below which I called externally.
The call was like this:
}
如果您想访问远程调用期间使用的属性(调用的参数和/或 AsycToken),您可以使用闭包。 只需将调用方法内的结果事件处理程序定义为闭包即可。 然后它可以访问调用函数中的任何变量。
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.
如果我正确地阅读了您的问题,您正在尝试弄清楚如何访问
ResultEvent
返回的实际数据?如果是这样,假设您已正确进行调用并且已以您期望的格式返回数据:
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:
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.