1

我对正在编写的程序的特定结构有一些疑问。

我正在使用远程对象对 Rails 方法进行远程调用(使用 WebOrb)。问题出现在我取回数据的方式上。

基本上我有一个函数 getConditions,我在其中向远程调用添加了一个事件侦听器,然后进行远程调用。但是,我想做的是在 getConditions 中取回该数据,以便我可以返回它。这是一个问题,因为我只访问事件处理程序中的事件结果数据。以下是描述此问题的一些基本代码:

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

我怎样才能实现这种数据周转?

4

3 回答 3

2

你这样做

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

}
于 2011-01-28T21:02:22.767 回答
2

flex 中的远程调用始终是异步的,因此您将无法调用 getConditions() 并在那里等待结果。您必须使用函数闭包来处理结果,或者通过在其他地方声明的事件处理程序或在 getConditions() 中立即创建的动态处理程序,如下所示:

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

这样做的好处是您将能够“看到”传递给 getConditions() 的参数或函数闭包中 addEventListener() 之前发生的任何逻辑的结果。但是,与声明显式函数(出于这个确切原因)相比,这会对性能造成轻微影响。

我还应该补充一点,这样做需要您自己进行清理,以确保您没有为每个请求创建新的侦听器。

于 2011-01-28T21:12:31.520 回答
0

您可以像这样使用呼叫响应程序:

<s:CallResponder id="getOperationsResult"/>

然后使用这些行从 get 操作中获取结果

getOperationResult.token = remoteObject.getOperation();

这将创建调用并返回结果并将其存储在 getOpresult

当你想访问这个你可以调用那个令牌或 getOperationResult.lastResult

希望对克里斯有所帮助

于 2011-01-28T21:05:16.820 回答