tags:

views:

37

answers:

1

I have a GWT App where I need to call a webservice to check whether the user signed in is an administrator - and then set the admin Div to visible (hidden by default) if the web service returns true.

The problem is the program passes the check before the web service can return the result. It's looking something like this

public class ModelClass{

boolean isAdmin = false;


  public ModelClass(){
//Call webservice in constructor, if returns true, set isAdmin to true via setter
  }
}

Then, in my widget, I create an instance of the ModelClass and then in the last step before the page finishes loading, I check the isAdmin property to see if it's true, if so - set the Admin panel to visible. No matter how early I try to make the call, and how late I check the property, the admin check always happens before the web service response returns.

I've tried change listeners - but they only apply to widgets. I tried rigging the property as a label and using a click event by calling click() on the label from the web service response.

Nothing seems to work - does anyone have any ideas?

+2  A: 

If you are using a callback mechanism, you will have to do it in the callback function.

e.g. If you are using the GWT's request builder, You will have to do it in onResponseReceived of your request callback:

   public ModelClass() {
      isAdmin();
   }

   private void isAdmin() {
        RequestBuilder builder = new RequestBuilder(
                RequestBuilder.GET, webserviceurl);
        try {
            request = builder.sendRequest(null, new RequestCallback() {

                public void onResponseReceived(Request request, 
                        Response response) {
                    int code = response.getStatusCode();

                    if(code >= 400) {
                        Window.alert(response.getStatusText());
                        return;
                    }

                    if(code == 200)  {
                         // if admin is logged in
                        // hide your div
                    }
                }

                public void onError(Request request, Throwable exception) {
                   Window.alert("Error checking admin status");
                }

            });
        }catch(RequestException re) {
            Window.alert("Error checking admin status");
        }
    }
naikus