I create a WebDialog to show the coordinates of point. It does not work. But when I add a statement “UI.message”, the coordinates of point was shown in the WebDialog after click OK of the message box. I am confused. The Code is below:
The webdialog needs time to open, and there is not yet any HTML immediately after you do show.
The method UI::WebDialog#show is asynchronous, meaning it returns to Ruby (and continues with the next line) before it is finished. In contrast, UI.messagebox is synchronous and returns only after you have clicked “OK”. By then, the webdialog was ready and could display the coordinates.
To work with asynchronous methods, one usually uses callbacks. That means when the HTML is ready (DOMContentLoaded), you notify (with JavaScript) your Ruby code and run a callback that you have registered before.
The old UI::WebDialog (not UI::HTMLDialog) allows to register the callback directly with the show method as a Ruby code block: