I'm a Flex newbie and I've searched both StackOverflow and Google'd but can't seem to figure out this (simple) problem with Flex/ActionScript 3 Asynchronous programming. I have a PHP service (Zend) that inserts a row into the table. What I would like to do is be able to call the service twice consecultively with different row values, and get back the new IDs (primary keys) returned by the service. Then I display the new IDs as 2 Alerts.
The problem is when I can the service twice in a row, I only get an Alert.show() for the last and most recent service call. I don't know how to access the result of the first.
I recognize I can reconfigure my Flex code and PHP service to accept an array of objects and just send a single PHP service call for both and receive back an array object with the results, but I'm also trying to get a better understanding in general how AsyncToken is working and how to access old results that I need. Is each use of {serviceResult}.token overwriting my previous result?
<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"
// ...
Here is my related code where I want to set up 2 default folders for new users "Home" and "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 fu开发者_如何学Pythonnction 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
}
and here are my event handlers, and I want an Alert box for each new ID to pop up:
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
}
and here are my mx codes for callresponder and service:
<s:CallResponder id="createFoldersResult"
result="createFoldersResult_resultHandler(event)"/>
<foldersservice:FoldersService id="foldersService"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
showBusyCursor="true"/>
Only '101' (the result of second service call) is triggering an Alert. Why is this?
Thank you!
Your code is overriding the token that createFoldersResult should respond to. A proper code would be:
protected function createFoldersFunction(item:Folders):void
{
var token:AsyncToken = foldersService.createFolders(item);
var responder:Responder = new Responder(createFoldersResult_resultHandler, someFaultHandler)
token.addResponder(responder);
}
another option would be set up the result handler for createFolders directly in the mxml, so your foldersservice:FoldersService would be:
<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>
then you don't need createFoldersFunction, you can call foldersService.createFolders directly.
精彩评论