I don’t get it…
Now I created a separate html file too. (test.html
beside main.rb
)
From the Ruby side I want to send an array (with two string in it). So I’m converting the array to json
Then I’m sending it after the html loaded. (1 sec Timer for now)
What is strange for me it is “arriving” to JavaScript as Array
(according to console log in Devtools). Why? I assuming I have to parse back?!
so I can display it without any use of JSON.parse()
.
What I’m missing here? What is wrong?
# encoding: UTF-8
#dezmo_test/main.rb
module DezmoTest
extend self
def strange_names
model = Sketchup.active_model
layername = model.layers[1].name
pagename = model.pages.first.name
display_result_html( [layername, pagename] )
end
def display_result_html(array)
p array
argument_js = array.to_json
p argument_js
@dialog = UI::HtmlDialog.new(
:dialog_title => "Test Dezmo 2022",
:width => 300,
:height => 100,
:left => 100,
:top => 150,
:style => UI::HtmlDialog::STYLE_DIALOG
)
@dialog.set_file(File.dirname( __FILE__ ) + "/test.html")
@dialog.show
UI.start_timer(1, false) {
@dialog.execute_script("update(#{argument_js});")
}
end
end
DezmoTest.strange_names
<!DOCTYPE html>
<html>
<body>
<div>Test</div>
<div id="example"></div>
<script>
function update(json){
console.log(json);
<!-- var data = JSON.parse(json); -->
var data = json;
document.getElementById("example").innerHTML = data.join(' ');
}
</script>
</body>
</html>
Ps (In a realty I’m trying to send an array, containing hashes. The hash values can be the string with the above mentioned characters, or an array with a same structure as the parent… like in this topic )