Synchronizing Multiple WebDialogs
-
Anybody have a solution for the general problem: how can I get WebDialogs to talk to each other?
Or the specific problem? I need this sequence of events:
Ruby opens WDFirst.
Ruby opens WDSecond.WDFirst displays button. User clicks button.
JS in WDFirst calls Ruby. Ruby prepares and returns JSON packet.
WDSecond receives "Ready" message. (How? email? Pony Express?)
WDSecond calls Ruby; Ruby prepares and returns second JSON packet.
-
@martinrinehart said:
WDSecond receives "Ready" message. (How? email? Pony Express?)
http://code.google.com/apis/sketchup/docs/ourdoc/webdialog.html#execute_script
It's a bit awkward - as you have to compile a Javascript snippet that gets evaluated. I used this to build a JSON string that's sent as an argument to a function. I think you had to properly escape the data as it gets parsed a few times. -
@thomthom said:
http://code.google.com/apis/sketchup/docs/ourdoc/webdialog.html#execute_script
I don't understand how this solves the problem.
I believe it's true that a WebDialog you instantiate via UI::WebDialog.new is unable to execute_script(). Only the WebDialog returned via an action callback can do so. This does not get an alert:
# t.rb require 'sketchup' wd = UI;;WebDialog.new "foo", true, "", 100, 100, 100, 100, true wd.execute_script( 'alert( "hello from foo" );' )
If this is true, it's another thing to fix in the docs. Anyone care to confirm or contradict?
I have one idea, but it's a candidate for a Kolossal Kludge award.
-
I've had no problems using
.execute_script
outside any event callbacks.In that test of yours - there is no HTML. Maybe that prevents any scripts from executing..?
-
Also - in that test - the dialog isn't shown...
-
Yup - you need HTML content and show the dialog.
wd = UI;;WebDialog.new "foo", true, "", 100, 100, 100, 100, true wd.set_html "<html><body>Hello World</body></html>" wd.show wd.execute_script( 'alert( "hello from foo" );' )
-
@thomthom said:
Yup - you need HTML content and show the dialog.
> wd = UI;;WebDialog.new "foo", true, "", 100, 100, 100, 100, true > wd.set_html "<html><body>Hello World</body></html>" > wd.show > wd.execute_script( 'alert( "hello from foo" );' ) >
I get the dialog, but no alert. Do you get an alert?
-
Yes - that exact sequence give me an alert box.
Mind you - I tried this in the console. Maybe the dialogue isn't immediately ready for execute_script is you run this as a continuous script. Maybe some delay is required. (Or have your webdialog JS send a "ready" message back to SU once it's loaded.)
sigh Webdialogs isn't smooth sailing... Even though I'm comfortable with HTML/CSS/JS - the communication between SU and webdialogs is .. well... bleh! -
@thomthom said:
Yes - that exact sequence give me an alert box.
Mind you - I tried this in the console.
Bingo! In the console I get the alert. Now maybe I can bury my Kolossal Kludge.
Thanks.
-
Ok - so when communicating with Webdialogs:
- Initialize a Webdialog object.
- Populate with HTML and display the dialog
- Wait for a "ready" signal from the dialog before attempting any communication
This sounds like the check-list you have to follow in order to reliably establish communication? One for your Webdialog gotacha-thread.
-
One thing I've learned is to include a callback at the end of my html telling the script that the WebDialog is ready to receive communication, as you mentioned, and as I have mentioned in other threads.
-
@rickw said:
One thing I've learned is to include a callback at the end of my html telling the script that the WebDialog is ready to receive communication, as you mentioned, and as I have mentioned in other threads.
Precisely, if you need a web dialog the web dialog has to run the show. Anything else is a pas de deux to nowhere.
-
@rickw said:
One thing I've learned is to include a callback at the end of my html telling the script that the WebDialog is ready
I don't understand. Maybe you are solving a different problem. Here's my situation, simplified to a single WebDialog.
User clicks button. (There is no button to click until the <body> is built, so calls from a button click can't happen prematurely - I think.) JS calls Ruby callback requesting data. Ruby fetches data and returns it to JS.
I think the "Ruby, data please" call also implies "Ruby, I'm ready". No? What am I missing?
-
Ah! Didn't notice that the
.show
and.show_modal
where block methods. That's useful to know. Saves that extra callback from the webdialog. -
This gets an alert from a file, at least for me:
wd = UI;;WebDialog.new "foo", true, "", 100, 100, 100, 100, true wd.set_html "<html><body>Hello World</body></html>" wd.show{ wd.execute_script( 'alert( "hello from foo" );' ) }
What problem are you having, exactly?
-
@cjthompson said:
What problem are you having, exactly?
Thomthom didn't have a problem. I did.
The problem was that if you called wd.execute_script() after wd.show() (instead of in a block passed to wd.show() ) it failed.
Thank you for your clear code. Do you happen to know any more about when the show() block is actually executed than what the docs report?
Thanks.
-
not really. I pretty much just stumbled across it, and saw that it worked.
I don't really know much about DOM events or even html in general. Maybe you know how to execute a script that will show what state the browser is in, and execute that on show?
-
@martinrinehart said:
Do you happen to know any more about when the show() block is actually executed than what the docs report?
I assume it would be possible to devise some tests to track when it happens, using a javascript logger or something.
-
@jim said:
I assume it would be possible to devise some tests to track when it happens, using a javascript logger or something.
Jim, this is a sensitive and tender area! My VisMap did not originally support the Mac because of timing/thread issues. The simple answer was to postpone the start of Ruby/JS chat by forcing the user to click an otherwise unnecessary startup button.
If you find a tool that actually lets you step through the states of the browser, it would be invaluable.
-
@martinrinehart said:
If you find a tool that actually lets you step through the states of the browser, it would be invaluable.
... for whom?
"This is commercial, for-profit work. At some point there will be a send-me-money feature added. At this point (mid-summer, 2009) that feature does not exist. The whole price right now is feedback. Tell me what works and what's unclear. Major mistakes and minor typos." [source]
Free content/editing from feedback seems as bad as free designs from competitions (much despised around here - example).
Advertisement