I am trying to send a text from ruby to an html dialog but I’m not understanding how to do it.
I have this in my ruby code
@cadenadetexto = "Hello World"
dialog.add_action_callback("trasladainfo01"){|action_context|
cadena = "#{@cadenadetexto}"
}
Then I have this in my js code
function traspasaInfo () {
sketchup.trasladainfo01()
let textToHtml = cadena;
document.getElementById("text").innerHTML = textToHtml;
}
And finally I have this in my html code
<body onload="traspasaInfo();">
<p>I want to write <span id="text"></span></p>
</body>
But when I execute the code I only obtain this:
What need I to do to obtain the text “Hello World” from @cadenadetexto ?
dezmo
July 5, 2022, 8:08am
2
You need to use e.g. the onCompleted
callback on JS to receive the return value from Ruby…
like in your other topic:
The onCompleted callback on JS should be able to receive the return value from Ruby, so if you pull data from JS you don’t need to use execute_script to push the result back from Ruby:
Something like this (untested):
Ruby:
dialog.add_action_callback("layers_to_form") {|action_context|
# This returns a JSON string from this callback.
Sketchup.active_model.layers.map(&:name).to_json
}
JavaScript :
function listOfLayers () {
sketchup.layers_to_form({
onCompleted: function(json) {
…
or “less elegantly”: use execute_script
to push the result back from Ruby like my first post on that topic…
Thanks @dezmo I did the last time but I didn’t realize what I was doing exactly.
This is the js code if someone needs it
function traspasaInfo () {
sketchup.trasladainfo01({
onCompleted: function(cadena) {
let textToHtml = cadena;
document.getElementById("text").innerHTML = textToHtml;
}});
}
1 Like
It is important to note that the cadena
object in Ruby is not the same as the cadena
parameter in JavaScript. It is the value of the text that is passed back into the JS-side by the on completion feature.
So what I’m getting at is that your Ruby-side callback block does not need to make the cadena
assignment. All the block needs to do is return the @cadenadetexto
object. Ie …
@cadenadetexto = "Hello World"
dialog.add_action_callback("trasladainfo01") { |action_context|
@cadenadetexto
}
And actually you need not make the textToHtml
assignment in JavaScript either …
function traspasaInfo () {
sketchup.trasladainfo01({
onCompleted: function(cadena) {
document.getElementById("text").innerHTML = cadena;
}});
}