Multiple Action Callbacks?
-
The docs don't say you can do this, but in my first test, it worked. Anybody using this in production code?
# selectionInfo.rb require 'sketchup.rb' # Create the WebDialog instance wd = UI;;WebDialog.new("Selection Info", false, "Selection Info", 200, 200, 200, 200, true) # Attach an action callback wd.add_action_callback("refresh") do | js_wd, ignored_param | total_selected = Sketchup.active_model.selection.length script = "rubyReturned("+ total_selected.to_s + ")" js_wd.execute_script( script ) end wd.add_action_callback("lie") do | js_wd, ignored_param | js_wd.execute_script( "rubyReturned( 'A million' )" ) end # Find and show our html file wd.set_file( 'c;/models/rubies/selectionInfo.html' ) wd.show()You'll need to change that hard-wired path to your own.
<!-- selectionInfo.html --> <html> <head> <script> function rubyCalled( nop ) { query = 'skp;refresh@' + nop; window.location.href = query; } function rubyLies( nop ) { query = 'skp;lie@' + nop; window.location.href = query } function rubyReturned( value ) { var message = value + " items selected."; document.getElementById('output').innerHTML = message; } </script> </head> <body onload='rubyCalled("refresh")'> <div align=center> <h3 id="output"> </h3> <button onclick="rubyCalled('this_is_ignored')">Refresh</button> <button onclick="rubyLies('this_too_is_ignored')">Tell Lie</button> </div> </body> </html>(Thank you, Scott Lininger, for the original code.)
-
I was not aware of any limitation on the number of callbacks. Out of curiosity, where did you read that?
-
@jim said:
I was not aware of any limitation on the number of callbacks. Out of curiosity, where did you read that?
I had assumed that you needed a switch in "the" action callback for various options. Didn't know that the "get_data" part of 'skp:get_data@'... from Scott's how-to was a variable.
And where does one report that the invaluable link to Scott's article in the doc is simple text, not a link?
-
One technique I have used is to pass the name of the Ruby callback to the Javascript. In many cases, you can use a single Javascript function to call any number of Ruby callbacks.
function callRuby(callback_name, parameters) { window.location="skp;"+callback_name+"@"+paramters; } <input type="button" onclick="callRuby('draw_square', 10);" value="Draw a Square"> -
@jim said:
One technique I have used is to pass the name of the Ruby callback to the Javascript. In many cases, you can use a single Javascript function to call any number of Ruby callbacks.
Neat trick, and that tells me exactly what I wanted to know. Now where to we go to get the docs to say "add one or more callbacks ..."?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement