我正在尝试理解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的语法和功能。
发布于 2009-08-04 16:48:51
如果您希望访问远程调用期间使用的属性(调用和/或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);
}发布于 2010-07-13 21:53:41
我甚至不需要闭包。我添加了一个类,如下所示,我在外部调用了它。
电话是这样的:
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));
} }
发布于 2011-12-21 22:41:11
虽然被接受的答案将完成原始提交者想要的,但它实际上并没有回答所提出的问题。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
}https://stackoverflow.com/questions/1228255
复制相似问题