Tum
Tum

Reputation: 3652

What is the simplest way to show "loading indicator" during an RPC call in GWTP?

I am using GWTP to build my webapp. Here is my problem. I have a customer page (CustPresenter). When that page is visited, it will call & load Data from Database.

It may take a while to load all data, so I want my current Browser locked so that the user can not click any things while data has not finished downloading yet.

So here is what I am thinking. I want to show a DialogBox right before I make the RPC call & hide it when the call finishes. Look at the below code:

public class CustPresenter extends
    Presenter<CustPresenter.MyView, CustPresenter.MyProxy> {

    private DialogBox loadingDialogBox=new DialogBox();

    @Inject DispatchAsync dispatchAsync;

    @Override
    public void prepareFromRequest(PlaceRequest request){
         super.prepareFromRequest(request);
         loadingDialogBox.show();
         GetData getDataAction=new GetData();
         dispatchAsync.execute(getDataAction, getDataCallback);

    }

    private AsyncCallback<GetDataResult> getDataCallback=new AsyncCallback<GetDataResult>(){

        @Override
        public void onFailure(Throwable caught) {
            loadingDialogBox.hide();
        }

        @Override
        public void onSuccess(GetVerbFromTripleResult result) {
             //show data here (it may take long time)
             loadingDialogBox.hide();
        }
   };
}

However, when runing, the dialogbox only shows up for 1 second & then disappears. Right After that all the UiBinder Guis show up on the Customer Page & user can click any buttons on the page while the data has not finished downloading yet. This is not a correct behavior.

I want to do something like this http://sqlfiddle.com/#!2/a2581/1. As u can see, when u first time open that link, a loading panel popups & u can't click anything until all guis show up.

I also tried to make a Custom AsyncCallback, but nothing happened

public abstract class AsyncCall<T> implements AsyncCallback<T> {


    PopupPanel myp=new PopupPanel();
    /** Call the service method using cb as the callback. */
    public AsyncCall()
    {
        myp.setTitle("loading");
        myp.show();

    }

    public final void onFailure(Throwable caught) 
    {    
        myp.hide();

        onCustomFailure(caught); 
    } 

    public final void onSuccess(T result) 
    {       
        myp.hide(); 

        onCustomSuccess(result);     
    }
    /** the failure method needed to be overwritte */   
    protected abstract void onCustomFailure(Throwable caught);  

    /** overwritte to do something with result */
    protected abstract void onCustomSuccess(T result); 

}

So, why do I have this problem?

How to fix it?

Or generally, What is the simplest way to show "loading indicator" during an RPC call in GWTP?

Upvotes: 2

Views: 1395

Answers (1)

Tum
Tum

Reputation: 3652

I found a cool solution, that is we have to use WidgetPresenter with PopupPanel (say LoadingPresenter). So in my Custom AsyncCall class, i need fire loadingPresenter

public abstract class AsyncCall<T> implements AsyncCallback<T> {


    /** Call the service method using cb as the callback. */
    public AsyncCall()
    {
        Utility.eventBus.fireEvent(new ShowLoadingEvent(true));
    }

    public final void onFailure(Throwable caught) 
    {    
        //myp.hide();
        Utility.eventBus.fireEvent(new ShowLoadingEvent(false));
        onCustomFailure(caught); 
    } 

    public final void onSuccess(T result) 
    {       
        //myp.hide(); 
        Utility.eventBus.fireEvent(new ShowLoadingEvent(false));
        onCustomSuccess(result);     
    }
    /** the failure method needed to be overwritte */   
    protected abstract void onCustomFailure(Throwable caught);  

    /** overwritte to do something with result */
    protected abstract void onCustomSuccess(T result); 

}

To make RPC Call, simple use this:

  private AsyncCall<GetDataResult> getDataCallback=new AsyncCall<GetDataResult>(){

        @Override
        public void onCustomFailure(Throwable caught) {

        }

        @Override
        public void onCustomSuccess(GetVerbFromTripleResult result) {
             //show data here (it may take long time)

        }
   };

and then it run really smoothly.

Upvotes: 3

Related Questions