I\'m trying to create a modal confirmation dialog box. I\'d like it to work like Window.confirm(\"\")
, where I can just call it, and get a boolean response.
You're not going to be able to have it work in exactly the same way as Window.confirm()
. The problem is that all of the javascript in a web page runs in a single thread. You'll notice that as long as a standard confirm dialog is open, the rest of the page goes dead. That's because the one javascript thread is blocked, waiting for confirm()
to return. If you were to create a similar method for your dialog, as long as it was waiting for that method to return no user generated events would be processed and so your dialog wouldn't work. I hope that makes sense.
The best you will be able to do is similar to what the GWT library does for RPC calls -- the AsyncCallback
interface. You could even reuse that interface yourself, or you might prefer to roll your own:
public interface DialogCallback {
void onOk();
void onCancel();
}
Instead of Window.confirm(String)
, your method signature will be more like Dialog.confirm(String,DialogCallback)
. Then your dialog just keeps a reference to the callback that's passed in, and where you have // do something
in your code you make calls to onOk
and onCancel
.