These are chat archives for opal/opal

19th
Nov 2018
Bernhard Weichel
@bwl21
Nov 19 2018 09:08

Hi I need an advice: I initialize the contoller of my app with

Document.ready.then do |ready|
  uicontroller = Controller.new
  %x{
     window.zupfnoter=#{uicontroller};
  }
end

In order to have a global access to the Controller -instance I assign it to window.zupfnoter. The Controller produces SVG code in a worker which returns the SVG as a string. I would like to declare handlers in this string. I figured out the JS source code requird in the SVG code to access a method of an object stored in an instance variable of my controller:

      onclick = %Q{onclick="zupfnoter.tune_preview_printer.$_clickabcnote(evt, '#{id}')"}
      %x{
          #{@root}.out_svg('<rect ' + #{onclick}' ...);

which eventually produces

<rect onclick="zupfnoter.tune_preview_printer.$_clickabcnote(evt, '_note_436_438_')"></rect>

even if if works, I am not sure if this is the most elegant and robust approach:

  1. It depends on the global variable (zupfnoter). I would prefer to access the instance of the controller via something like Opal.top.uicontrollersuch that I do not need the global variable.

  2. It depends on the internals of the opal compiler. I would like to get the javascript name of the handler without knowledge of the internals how Ruby code is converted to javascript.

it should be something like

 onclick = %Q{onclick="#{JS.methodcall(uicontroller.tune_preview_printer._clickabcnote)}(evt, '#{id}')"}
Bernhard Weichel
@bwl21
Nov 19 2018 09:23

I found out that if I use

Document.ready.then do |ready|
  @uicontroller = Controller.new
end

I can subsequently find my controller instance by

Opal.top.uicontroller

but it still depends on internal knowlege. ...