我试图了解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的语法和功能。
答案 0 :(得分:4)
我甚至不需要关闭。我在下面添加了一个类,我在外面打电话。
电话是这样的:
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));
}
}
答案 1 :(得分:2)
虽然接受的答案将完成原始提交者想要的内容,但实际上并未回答所提出的问题。 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
}
答案 2 :(得分:1)
如果我正确地阅读了您的问题,您正试图弄清楚如何访问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有一个fault属性,可以返回故障信息。
编辑:更改_handleTestResult()
中的代码以回应戈登波特的评论。
答案 3 :(得分:1)
如果要访问远程调用期间使用的属性(调用和/或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);
}