MayoMan
MayoMan

Reputation: 4917

Will an gwt rpc call ALWAYS callback to either my onsuccess or onfail methods?

I have implemented the suggestion in this post and disable my button after the the first click. How to prevent DoubleSubmit in a GWT application? What i want to know is with my button reenabled in both my fail and success methods will it always get reenabled. Also is there any place i can put some code that i always want executed when the server replies as opposed to duplicating it in both fail and success methods

Upvotes: 1

Views: 451

Answers (1)

enrybo
enrybo

Reputation: 1785

I do think that either onSuccess() or onFailure() will be called every time.

As for having a place where you can put code that will always run when getting a response to the server you could just create an AsyncCallback which has the code in it's onFailure() and onSuccess() methods. Then you can just extend that AsyncCallback everytime you create an AsyncCallback.

public MyAsyncCallback<T> extends AsyncCallback<T>(){

    @Override
    public void onFailure(Throwable caught){
        //Do something
        onResponse()
        failed(caught);
    }

    @Override
    public void onSuccess(T result){
        //Do something
        onResponse()
        succeeded(result);
    }

    public void onResponse(){
        // Do something or nothing by default
    }

    public abstract void failed(Throwable caught);

    public abstract void succeeded(T result);

};

Whenever you want to create an AsyncCallback just use MyAsyncCallback:

AsyncCallback callback = new MyAsyncCallback(){

    @Override
    public void failed(Throwable caught){
        //Do something
    }

    @Override
    public void succeeded(T result){
        //Do something
    }  

    // Optionally override onResponse() if needed
    @Override
    public void onResponse(){
        //Do something
    }

}

Upvotes: 3

Related Questions