Making multiple asynchronous calls to fetch result from multiple services in the single click of a button in flex?

StackOverflow https://stackoverflow.com/questions/4541281

سؤال

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.

هل كانت مفيدة؟

المحلول

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???

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top