• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

When Ruby Can't Execute_Script

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 4 Posters 1.0k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    MartinRinehart
    last edited by 20 Aug 2009, 13:56

    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.

    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

    1 Reply Last reply Reply Quote 0
    • J Offline
      Jim
      last edited by 20 Aug 2009, 14:21

      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.

      Hi

      1 Reply Last reply Reply Quote 0
      • M Offline
        MartinRinehart
        last edited by 20 Aug 2009, 16:44

        @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
        

        Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

        1 Reply Last reply Reply Quote 0
        • R Offline
          RickW
          last edited by 20 Aug 2009, 19:16

          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>
          

          RickW
          [www.smustard.com](http://www.smustard.com)

          1 Reply Last reply Reply Quote 0
          • M Offline
            MartinRinehart
            last edited by 20 Aug 2009, 20:20

            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>.)

            Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by 8 Nov 2009, 16:51

              @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.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • M Offline
                MartinRinehart
                last edited by 9 Nov 2009, 23:49

                @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?

                Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement