views:

3738

answers:

3

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.

+1  A: 

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:

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
  }
}

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.

Hooray Im Helping
Ok I think this partially makes sense. But I need to be more clear I think. Assume the above code and then assume I make the following function call somewhere in my project:testSerivceCall(personObject, "John");Now inside my service result how get access to the actual string "John"?Perhaps I want to do some very specific if it is a person object that contains "John".Does this make sense?
Gordon Potter
+1  A: 

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.

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);
}
Chetan Sastry
Ok, this makes sense thanks! Still getting a handle on closures.
Gordon Potter
+2  A: 

I did not even need closures. I added a class as below which I called externally.

The call was like this:

 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));
   } 

}

will edwards