Remove_observer Method

Most coders will write a wrapper class around their use of a web dialog, so the js command would be wrapped in an instance method.

But you can define a singleton method directly upon the @dlg001 object …

def @dlg001.fire_js(data_hash)
  json = data_hash.to_json # has embedded double quotes
  js_command = %[javascript_set_data('#{json}');]
  @dlg001.execute_script(js_command)
end

It is likely best if you use data hashes and send them over as JSON.
on the JS side you convert the JSON string to a JS Object …

  // an Object to hold data ...
  var myObject = {}

  var javascript_set_data = function (json) {
    myObject = JSON.parse(json);
  }

Oh and I forgot, in order for the observer to “know” the dialog object you’d pass it’s reference into the observer’s constructor …

@spy = MySelectionObserver.new(@@dlg001)

… and then you assign the dialog reference to an internal instance variable so that you can call the singleton method …

class MySelectionObserver < Sketchup::SelectionObserver

  def initialize(dlg)
    @dlg = dlg # @dlg is now a reference to the dialog
  end

  def callback_name(selection)
    data_hash = {}
    # in some callback ... calc some data
    @dlg.fire_js(data_hash)
  end

end
1 Like