views:

494

answers:

2

How can I centralize management of a "loading" icon for GWT async RPC calls? I'm looking for a way to have every async call automatically kick off a timer. When the timer fires, if the RPC has not yet completed, a "loading" icon should be displayed. When the RPC completes (either onSuccess() or onFailure()) the loading icon should be removed.

It's tedious to do this manually for each call, and in fact quite easy to get it wrong and leave the user with a stuck UI.

Could generators be used for this? Just to be clear- I'm not looking for code to display a dialog or icon; I'm looking for a way to centralize management of such a dialog/icon.

+3  A: 

Hi,

Maybe I look at it in a more simple way. But we have similar behavior in our applications and we just implement it using a decorator pattern. The decorator takes care of showing the loading UI (in our case it shows right away), and on return makes sure that the UI is removed again.

Our invocations look a bit like this:

rpc.dosomething( params, Modality.appModal( new AsyncCallback<String>() {
  public void onSuccess( String pValues ) {
    ... handle success ...
  }

  public void onFailure( Throwable pCause) {
    ... handle failure ....
  }
}));

Modality.appModal returns an AsyncCallback that does the UI part and after success or failure dispatches to the AsyncCallback given as parameter in the invocation.

Is it worth going further with code generators ? It is implemented once and used everywhere without further thinking.

David

David Nouls
+1  A: 

You can find some answers here: Automatic ‘loading’ indicator when calling an async function.

I agree with others on this page that it is not worth using code generators for this.

David Tinker
Thanks David. I Guess I didn't do a good enough job looking for previously asked questions.
Caffeine Coma