Flex 事件调度

发布于 2024-10-15 05:26:19 字数 645 浏览 9 评论 0原文

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

我正在使用远程对象对 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;
    }

如何实现此数据周转?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2024-10-22 05:26:20

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() 之前发生的任何逻辑的结果。然而,与声明显式函数(正是出于这个原因)相比,这会对性能造成轻微影响。

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

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.

一袭水袖舞倾城 2024-10-22 05:26:20

你这样做

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

}

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

}
不再让梦枯萎 2024-10-22 05:26:20

您可以像这样使用 Call Responder :

<s:CallResponder id="getOperationsResult"/>

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

getOperationResult.token = remoteObject.getOperation();

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

无论何时您想要访问它,您都可以调用该令牌或 getOperationResult.lastResult

希望帮助
克里斯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文