Node Button Event

I have a node with a button that has an onClick event

<script type="text/javascript">
    RED.nodes.registerType('connect', {
        category: 'ringbeller',
        color: '#8d6b94',
        defaults: {
            name: { value: "" },
            settings: { value: "", type: "configuration" }
        },
        inputs: 1,
        outputs: 1,
        icon: "file.png",
        label: function () {
            return this.name || "Connect to the Modem";
        },
        button: {
            enabled: function () {
                return !this.changed
            },
            onclick: function () { 
                console.log('CLICKED')
            }
        }
    });
</script>

How do I trigger this node to run, or cause it's input callback to fire, when this button is clicked?


Thanks

Look at the inject node code on node-red repo. It typically calls an endpoint that you create in the js file.

I searched the repo for inject, but only found tests for inject, not the node itself. Could you please share the link to the file?

Thank you

Hi @jeevan900929

Here is where the node creates the http admin endpoint: node-red/20-inject.js at fad8dcd304728014551da41186d25db9f7320abd · node-red/node-red · GitHub

And here is where it gets called: node-red/20-inject.html at fad8dcd304728014551da41186d25db9f7320abd · node-red/node-red · GitHub

1 Like

I have a connectToModem function in the node:

  function ConnectNode (config) {
    ...
    const connectToModem = function () {
      ... 
    }
  }

How would I refer to this function from the onClick callback function:

button: {
            enabled: function () {
                return !this.changed
            },
            onclick: function () { 
                   **Needs to be called from here**
                      ConnectNode.connectToModem() ?
            }
        }

Thanks

You call it from the http endpoint in the js file. When you look at the first link above you see the inject does something similar. There a function node.receive() gets called from the endpoint upon request.

Great, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.