GWT confirmation dialog box

前端 未结 2 1998
小蘑菇
小蘑菇 2021-02-19 23:17

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.

2条回答
  •  遇见更好的自我
    2021-02-19 23:52

    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.

提交回复
热议问题