Add_action_callback problem

the mothed is “def update_multiple(x)” in “class Html_Update < Sketchup::SelectionObserver”.
this method is binding to a HtmlDialog in “add_action_callback”.
when i callback from HtmlDialog, i get a “Error: #<NoMethodError: undefined method `update_multiple’ for Zhouxi::Indexz::Html_Update:Class>”

why? :rofl:

We cannot tell you unless you post some code that shows the problem.

To know how to post code correctly in forum, see:

Oh, wait …

The UI::HtmlDialog#add_action_callback's block itself creates the callback method defined in the UI::HtmlDialog dialog instance, not in another class. (Ie, the block defines a singleton method upon the particular dialog object instance.)

But you can always have the action callback’s block then call another object’s method if you wish.

module Zhouxi
    module Indexz
        class Html_Update < Sketchup::SelectionObserver

@@indexz={:dialog_title=> "Indexz",:scrollable=> true,:resizable=> false,:width=> 300,:height=> 300,:left=> 85,:top=> 100,:style=> UI::HtmlDialog::STYLE_DIALOG})

@@html= " 
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <html> <head></head> 
    <body onload='document.getElementById('xx').checked=true'>
            <input type='text' name='keys' value='' onchange='sketchup.shuffle('>
    <div class='wrap'> </div></BODY></html>

def update_multiple(x)

@@indexz.add_action_callback("shuffle") { |action_context,k| update_multiple(Sketchup.active_model.selection)}


thanks for reply
this is the part of code.
if i add self. before the method, no error , but a blank html , this is another problem i dont understand.

and it seem i cant add “self.” because this method is called frequently in

def onSelectionBulkChange(x)
  update_multiple(x) if x.size >1

i basicly wanna input some data in htmlDialog then call some methods and refresh it.
so, any way out? :rofl:

(1) Ruby uses 2 space indentation. This helps also minimize horizontal scrolling in forum snippets.

(2) All API observer classes are abstract.

  • This means that you gain nothing by subclassing them as they have no superclass functionality to pass down to subclasses.
  • All observer attach methods do no class checking at all. This means that any kind of object can be an observer, of any kind, at all times. Ie, hybrid observer objects.

So basically your use of the Html_Update as a class is not correct. You only create a class if your code will be making many instances of such class.

You don’t need a class here. Just use an extension submodule (ie, IndexZ) and extend it with itself …

module Zhouxi
  module IndexZ

    extend self

Now you methods can be called without the self. qulaification.

(3) For HTML, …

  • <meta> tags need to be within the <head> element.
  • Your HTML should always begin with a <!DOCTYPE html> tag. (ie, the 1st line before <html>.)

(4) Your add_action_callback must be called before you create the dialog with #show.

  • AND … if the user closes the dialog, and then your code opens it again (say, with a toolbar button) then you must reattach the action callback proc. (Closing a UI::HtmlDialog instance window breaks the link to the callback procs.)

(5) Outdenting SUCKS. Please don’t do this. Outdenting breaks the editor’s indent guidelines.

(6) Avoid the use of @@ class variable references unless you really understand how to use them.
In your case, you should really only be using @ instance variables of the extension module’s instance. (Meaning, a module is an instance of class Module. So it can have it’s own instance variables.)

Learn how to build hashes of Ruby object data, then create a JSON string of this hash.
Then pass the JSON string to the HTML using #execute_script.


so much to learn.thank you very much!

i did some tests today, and have some results, pls tell me if i am wrong. @DanRathbun

1. i think subclassing do gain something.
for example,i can add some new method and variables to the class without worrying conflicts.

2. add_action_callback seems cant call any method outside, but variables.
so in my case,i defined a new subclass (IndexzHtml) of UI::HtmlDialog,added all the methods in it.

    INDEXZ={:dialog_title=> "Indexz Pro",:scrollable=> true,:resizable=> false,:width=> 300,:height=> 300,:left=> 85,:top=> 100,:style=> UI::HtmlDialog::STYLE_DIALOG})
    INDEXZ.add_action_callback("shuffle") { |action_context,k| INDEXZ.update_multiple }

this works fine to me, but i dont know whether it have sideeffects?

You are on your own from here on. (Topic Muted.)

1 Like