When Ruby Can't Execute_Script
-
My Ruby didn't always talk to my JS via execute_script(). There's a gotcha that I'll describe here in the hopes that the next person bit by this one will find the answer with a quick Google.
If you need to know something about your model to create your webdialog UI, you will call Ruby from your JS. Fine. Ruby will return what you ask for via the execute_script method of the webdialog object it passes Ruby. Almost fine.
The problem occurs if your JS calls Ruby before the webdialog object is fully formed. If that's the case, the call to execute_script() is a NOP. Your Ruby calls your JS method but it is not called. Hmmm. You double-check spelling, puts() the exact script, ... (Guess how I know this?)
How do you get a fully-formed webdialog? Don't call Ruby from the <head> section, including from a separate .js file loaded in the <head> section. Either call Ruby from the last line of a <script> section immediately before </body> (or from a .js loaded there) or use the body's onload method.
-
The
show
method takes an optional code block that gets executed when the dialog is first displayed.http://code.google.com/apis/sketchup/docs/ourdoc/webdialog.html#show
You should be able to update your HTML elements in the .show method.
-
@jim said:
You should be able to update your HTML elements in the .show method.
That's what I'm using. Here's some code that people can use to experiment with positioning the JS call.
<!-- lsvm.html --> <html> <body> <div align=center> <h3 id="output"> </h3> <button onclick='rubyCalled("refresh")'>Refresh</button> </div> <script src='lsvm.js'> </script> </body> </html> <!-- end of lsvm.html -->
# lsvm.rb # showing JS to Ruby to JS communications require 'sketchup.rb' mess_num = 0 wd = UI;;WebDialog.new("Layer/Scene Visibility Map", false, "layer_scene_visibility_map", 200, 200, 200, 200, true) wd.add_action_callback("refresh") do | js_wd, ignored_param | mess_num += 1 script = "rubyReturned( '" + ignored_param + ' ' + mess_num.to_s + "' )" puts script js_wd.execute_script( script ) end wd.set_file( 'lsvm.html' ) wd.show() # end of lsvm.rb
// lsvm.js function rubyCalled( callback_name, string ) { if ( (typeof string) == 'undefined' ) string = ''; window.location.href = 'skp;' + callback_name + '@' + string; } function rubyReturned( data ) { document.getElementById( 'output' ).innerHTML = data; } rubyCalled( 'refresh' ) // end of lsvm.js
-
What I've done in the past is to put a callback at the end of my HTML code to let Ruby know the dialog is ready to go.
... </body> <script type="text/javascript"> window.location="skp;done_loading@" </script> </html>
-
Thanks, Rick.
The w3c insists that you put the <script>...</script> above the </body>. Please join in my subversive campaign against the w3c: http://www.MartinRinehart.com/articles/xhtml-strict-not.html
Doing it your way runs correctly in Chrome, Firefox, MSIE, Opera and Safari, however. (I still use <center>.)
-
@martinrinehart said:
The w3c insists that you put the <script>...</script> above the </body>. Please join in my subversive campaign against the w3c: http://www.MartinRinehart.com/articles/xhtml-strict-not.html
No no no. You should read up some more about separating content from layout in regards to web development. There are very good reasons why all best-practices articles discourages tag-soup.
-
@thomthom said:
@martinrinehart said:
The w3c insists that you put the <script>...</script> above the </body>. Please join in my subversive campaign against the w3c: http://www.MartinRinehart.com/articles/xhtml-strict-not.html
No no no. You should read up some more about separating content from layout in regards to web development. There are very good reasons why all best-practices articles discourages tag-soup.
You did not address the content of my article. Is your web the exclusive property of web professionals?
Are we drifting way off topic?
Advertisement