只需单击 Flex 中的按钮即可进行多个异步调用以从多个服务获取结果?

发布于 2024-10-09 00:54:17 字数 406 浏览 0 评论 0原文

在我们的一个项目中,我们在前端使用 flex,在后端使用 blazeds/java。它是预先编写服务的现有代码。我必须调用后端的 3 个服务(基本上是 3 个远程对象)并获取它们的结果并将结果存储在一个对象中,并在视图中显示该对象的数据。 现在在前端我们使用 Flex 和 Parsley Framework。我正在考虑以下方法。

1)为每个服务调用发出命令并将结果存储在共享对象(模型)中,然后在视图中显示该模型。在这种方法中,问题是某些其他网页需要某些服务,但它们不需要相同的模型。 我应该如何处理这种情况?我应该进行异步远程调用并获取结果,然后再次使用存储结果的事件对象调度和事件。

2)进行服务调用,等待结果,然后进行另一个调用,等待结果,然后进行其他调用,不确定这是否是正确的方法?

处理这种情况的最佳解决方案是什么?感谢您的帮助。

In one of our project we are using flex for front end, blazeds/java in the backend. Its an existing code where services are prewritten. I have to make calls to 3 services in the backend (basically 3 remote objects) and get their result and store the result in an object and show the data of this object in a view.
Now in front end we are using Flex and Parsley Framework. I was thinking of the following approaches.

1) Making commands for each service call and storing the result in a shared object (model) and then displaying this model in the view. In this approach the problem is some services are needed in some other web pages, but they donot need the same model.
How should i handle this scenario ? Should i make a asynchronous remote call and fetch the result and then again dispatch and event with the event object storing the result.

2) Making a service call , wait for the result then make another call and wait for the result and then make other call, not sure if this is the right way ?

What is the best solution to handle a scenario like this. Thanks for your help.

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

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

发布评论

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

评论(2

不气馁 2024-10-16 00:54:17

所以,我想我仍然对你的问题是什么感到困惑,所以我将尝试通过告诉我如果对后端进行三个不相关的调用我会采取的方法来回答它。

我会立即将他们全部解雇。由于 Flex 中的后端调用始终是异步的,因此它们将立即返回。这些调用中的每一个都将包含一个返回结果时的回调函数。因此,在伪(ish)代码中,它会是这样的:

makeRequest1(whenRequest1Finishes);
makeRequest2(whenRequest2Finishes);
makeRequest3(whenRequest3Finishes);

在这种情况下, makeRequest* 是一个方法,它知道服务器/机制是什么来进行调用并将回调到函数您已经定义了一些名为 whenRequest*Finishes 的地方。

假设,现在您的 UI 的三个不同部分与每个请求分开更新,我将在它们进入时填充 UI 的这些区域。不过,我不会简单地填充它们......我会给用户表示他们已经到达。这可以是数据“飞”入的微妙动画,也可以是数据显示时消失的等待指示器。您可以制作一个填充整个 UI 的等待指示器,而不是三个单独的等待指示器,该指示器在收到所有三个等待指示器之前不会消失。

不管怎样,我永远不会以串行方式进行调用,除非这些调用相互依赖。利用 Flex 中内置的异步性发挥您的优势,并行进行所有三个调用。

这是你要找的吗???

So, I guess I am still confused about what your problem is, so I will just try to answer it by telling the approach I would take if I had three unrelated calls to make to a backend.

I would fire them all off immediately after one another. Since backend calls in Flex are always asynchronous, they will return immediately. Each one of these calls will include a callback function for when the result is returned. So, in pseudo(ish)code, it would be something like this:

makeRequest1(whenRequest1Finishes);
makeRequest2(whenRequest2Finishes);
makeRequest3(whenRequest3Finishes);

In this case makeRequest* is a method that knows what the server/mechanism is to make the call and will call back to a function you have defined some place called whenRequest*Finishes.

Assuming, now that there are three different parts to your UI being updated separately from each one of these requests, I would populate those areas of the UI as they come in. I wouldn't simply populate them, though... I would give the user an indication that they have arrived. This can be a subtle animation of the data "flying" in or a wait indicator that disappears when the data shows up. Instead of three separate wait indicators, you might make one wait indicator that filles the entire UI that doesn't go away until all three are received.

Whatever the case, I would never make the calls in a serial way UNLESS the calls were dependent upon one another. Use the built-in asynchronicity in Flex to your advantage and make all three calls in parallel.

Is this what you are looking for???

月下凄凉 2024-10-16 00:54:17

您可以有一个对象来保存调用的三个结果,并使每个响应处理程序将其响应放入该对象中,并验证它们不是最后到达的调用。

示例:

ManyResult 类
  var 第一个结果:*
  var secondaryResult:*
  varthirdResult:*

  function getcomplete():Boolean { returnfirstResult&&第二个结果 &&第三个结果; 在每个处理程序中,

您将具有以下通用逻辑:
解析结果
则将解析结果放入 ManyResult 对象中

如果 ManyResult.complete:

method_which_glues_all_third_results_together()

如果这个问题很频繁,我不建议使用此解决方案,因为它很容易变得混乱。我建议您自己编写一些通用代码,以干净、标准的方式为您完成此操作。

You could have a single object which holds the three results of the calls and make every response handler put their response in the object and verify that they are not the last call to have arrived.

Example:

class ManyResult
  var firstResult:*
  var secondResult:*
  var thirdResult:*

  function get complete():Boolean { return firstResult && secondResult && thirdResult; }

In each handler you would have this generic logic:
parse result
put parsed result in ManyResult object

if ManyResult.complete:
  method_which_glues_all_three_results_together()

I would not recommend using this solution if this problem is frequent as it easily gets messy. I'd advise making yourself some generic code which does this for you in a clean, standard way.

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