What am I doing wrong with callbacks?

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?

You need to use e.g. the onCompleted callback on JS to receive the return value from Ruby…
like in your other topic:


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;
    }});
    
}

Thanks a lot @DanRathbun