Flex 事件调度
我对我正在编写的程序的特定结构有一些疑问。
我正在使用远程对象对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Flex 中的远程调用始终是异步的,因此您将无法调用 getConditions() 并在那里等待结果。您必须使用函数闭包来处理结果,要么通过您在其他地方声明的事件处理程序,要么通过在 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:
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.
你这样做
you do it like this
您可以像这样使用 Call Responder :
然后使用这些行从 get 操作获取结果,
这将创建调用并返回结果,将其存储在 getOpresult 中,
无论何时您想要访问它,您都可以调用该令牌或 getOperationResult.lastResult
希望帮助
克里斯
You could make use of Call Responder like so :
then use these lines to get the result from get operations
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