Flex Event Dispatching

361 Views Asked by At

I have some questions with a particular structure of a program I'm writing.

I'm using a Remote Object to make a remote call to a Rails method (using WebOrb). The problem arises in the way that I get my data back.

Basically I have a function, getConditions, in which I add an event listener to my remote call and then I make the remote call. However, what I want to do is to get that data back in getConditions so I can return it. This is a problem because I only access the event result data in the event handler. Here's some basic code describing this issue:

public function getConditions():Array
    {

        remoteObject.getConditions.addEventListener("result", onConditionResult);
        remoteObject.getConditions();
       //Here is where I want to get my event.result data back

    }
public function onConditionResult(event:ResultEvent):void
    {
        //Here's the data that I want
        event.result;
    }

How can I achieve this data turn-about?

3

There are 3 best solutions below

0
On BEST ANSWER

Remote calls in flex are always asynchronous so you won't be able to call getConditions() and wait there for the result. You have to use a function closure to process the results, either by means of an event handler than you declare elsewhere or a dynamic one created immediately within getConditions(), like so:

remoteObject.getConditions.addEventListener("result", function(event:ResultEvent):void {
  // Run the code that you would want to when process the result.
});
remoteObject.getConditions();

The advantage of doing the above is that you would be able to "see" parameters passed to getConditions() or the result of any logic that happened before addEventListener() in the function closure. This however, takes a slight performance hit compared to declaring an explicit function (for that exact reason).

I should also add that doing so requires you to clean up after yourselves to make sure that you are not creating a new listener for every request.

0
On

you do it like this

public function getConditions():Array
{

    remoteObject.getConditions.addEventListener("result", onConditionResult);
    remoteObject.getConditions();

}
public function callMyExtraFunction(data:Object):void
{
     //Here is where you want to get your event.result data back
}
public function onConditionResult(event:ResultEvent):void
{
    //Here's the data that you want
    var data:Object = event.result;
    callMyExtraFunction(data);

}
0
On

You could make use of Call Responder like so :

<s:CallResponder id="getOperationsResult"/>

then use these lines to get the result from get operations

getOperationResult.token = remoteObject.getOperation();

this creates the call and returns the result stores it in getOpresult

whnever u want to access this u can call that token or getOperationResult.lastResult

Hope that helps Chris