我是一个Flex新手,我已经搜索了StackOverflow和Google'd,但似乎无法找出这个(简单)问题与Flex/ActionScript 3异步编程。我有一个PHP服务(Zend),它将一行插入到表中。我想做的是能够使用不同的行值连续两次调用服务,并获得服务返回的新id(主键)。然后我将新id显示为2个Alerts。
问题是,当我连续两次调用服务时,我只得到最后一次和最近一次服务调用的Alert.show()。我不知道如何访问第一个的结果。
我认识到我可以重新配置我的Flex代码和PHP服务来接受对象数组,并且只需为两者发送一个PHP服务调用并接收返回结果的数组对象,但我也试图更好地理解一般情况下AsyncToken是如何工作的以及如何访问我需要的旧结果。是{serviceResult}的每次使用。令牌覆盖我之前的结果?
<s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
// ...
下面是我的相关代码,我想为新用户设置2个默认文件夹"Home"one_answers"temp":
// imports
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.rpc.Responder;
import spark.events.IndexChangeEvent;
import valueObjects.Folders;
// When the user clicks "Save" button, 2 new folders are created for a new user:
protected function pendingUserSaveButton_clickHandler(event:MouseEvent):void
{
// Create 2 initial user folders: Home and temp-taiwan
var t1Folders:Folders = new Folders();
t1Folders.users_email = tempUser.email;
t1Folders.name = t1Folders.description = "Home";
createFoldersFunction( t1Folders ); // assume returns folder ID = 100
var t2Folders:Folders = new Folders();
t2Folders.users_email = tempUser.email;
t2Folders.name = t2Folders.description = "temp";
createFoldersFunction( t2Folders ); // assume returns folder ID = 101
}
和这里是我的事件处理程序,我想要一个警报框为每个新的ID弹出:
protected function createFoldersFunction(item:Folders):void
{
createFoldersResult.token = foldersService.createFolders(item);
}
protected function createFoldersResult_resultHandler(event:ResultEvent):void
{
Alert.show("Folder #" + ((event.result as int) as String) + " created");
// Currently, I only get Alert saying "Folder #101 created".
// I want to see 2 Alerts - one for #100 and another for #101
}
和这里是我的mx代码呼叫响应和服务:
<s:CallResponder id="createFoldersResult"
result="createFoldersResult_resultHandler(event)"/>
<foldersservice:FoldersService id="foldersService"
fault="Alert.show(event.fault.faultString + 'n' + event.fault.faultDetail)"
showBusyCursor="true"/>
只有'101'(第二个服务调用的结果)触发警报。为什么会这样?
谢谢!
您的代码重写了createFoldersResult应该响应的令牌。正确的代码应该是:
protected function createFoldersFunction(item:Folders):void
{
var token:AsyncToken = foldersService.createFolders(item);
var responder:Responder = new Responder(createFoldersResult_resultHandler, someFaultHandler)
token.addResponder(responder);
}
另一个选项是直接在mxml中设置createFolders的结果处理程序,因此您的foldersservice: foldersservice将为:
<foldersservice:FoldersService id="foldersService"
fault="Alert.show(event.fault.faultString + 'n' + event.fault.faultDetail)"
showBusyCursor="true">
<mx:method name="createFolders" result="createFoldersResult_resultHandler(event)"/>
</foldersservice:FoldersService>
那么你不需要createFoldersFunction,你可以调用foldersService。直接createFolders。