Modal Dialog/Non-busy Wait

132 views
Skip to first unread message

melody

unread,
Sep 5, 2011, 1:01:29 PM9/5/11
to Google Web Toolkit
I wish to embed an asynchronous call to the server inside a method
that MUST NOT return until the server has responded. So I am looking
for a way to achieve a non-busy wait in GWT. I thought I could use a
modal popup dialog to stop the next line from being executed until the
dialog is closed and only after the response from server arrives.
Unfortunately the GWT modal dialog does not do what I thought it would
do -- which is block everything and wait at the line where the
PopupPanel.show() method is called. See method below

<code>
public boolean doPost(String url, String postData) {
RequestBuilder builder = new
RequestBuilder(RequestBuilder.POST, url);
final int STATUS_CODE_OK = 200;
final PopupPanel dlg = new PopupPanel();
dlg.setModal(true);
dlg.setGlassEnabled(true);
try {
builder.setHeader("Content-Type", "application/x-www-form-
urlencoded");
builder.sendRequest(postData, new RequestCallback() {
public void onError(Request request, Throwable
exception) {
dlg.hide();
}

public void onResponseReceived(Request request,
Response response) {
int li_status = response.getStatusCode();
if (li_status == STATUS_CODE_OK) {
//bravo
}
dlg.hide();
}
});
builder.setTimeoutMillis(3000);
dlg.show();
return true;
} catch (RequestException e) {
GWT.log(e.getLocalizedMessage());
}
return false;
}
</code>

I want the line

<code>
return true;
</code>

to be executed only after the dialog is closed just like what would
happen if I used Window.confirm to achieve the modality as shown
below.

<code>
Window.confirm("yes or no");
return true;
</code>


Any ideas on how I can achieve this.


Thanks,

Melody

Jens

unread,
Dec 8, 2012, 5:06:45 AM12/8/12
to google-we...@googlegroups.com
Window.confirm/alert is pretty much the only thing that can halt Javascript execution.

You have to refactor your code a bit. Instead of

public boolean doPost(String url, String postData)

you should use

public void doPost(String url, String postData, Callback<..., ...> callback) {

   dlg.show();
   builder.sendRequest(postData, new RequestCallback() {

       void onError(....) {
          dlg.hide();
          callback.onFailure(...);
       }

       void onResponseReceived(...) {
          dlg.hide();
          if(statusCode == OK) {
             callback.onSuccess(...);
          } else {
             callback.onFailure(...);
          }
       }

   });

}

So everyone who calls your doPost() method has to provide a callback in order to be notified when the post succeeds or fails. You have to think asynchronous :-) The only thing that makes sense to return in that case is the Request instance created by builder.sendRequest(...), so the calling code can cancel the request if needed:

public Request doPost(..., Callback callback) {
   dlg.show();
   return builder.sendRequest(...);
}

-- J.
Reply all
Reply to author
Forward
0 new messages