Set button state from js

Hi experts,

I created a node that connects to a websocket server. I was able to set the node state from the backend (javascript) using:

node.status({
          fill: "green",
          shape: "ring",
          text: "Connecting...",
        });

I also implemented a button to disconnect the websocket via the admin api.

defaults: {
active: {value: true},

button: {
            toggle: "active",
            onclick: function() {
                var node = this;
                $.ajax({
                    url: 'notification/' + node.id +'/'+(node.active?"enable":"disable"),
                    cache: false,
                    dataType: 'json',
                    method: 'GET',
                    success: function (data) {
                        console.log("Success: " + JSON.stringify(data));
                    }
		        });
            }
        }

When I press the button the connection is dropped and the status is displayed as disconnected (cool). Not working is the other way around. The connection drops from the server (btw status I can set correctly) but the button is now out of sync.

I tried to set the button state from javascript backend via

node.config.active = true;

But that has no effect. Any ideas?

Thanks and kind Regards,
Marco

This is very confusing as you haven't really told us the full picture. Where, for example, is the button being shown? In the Editor, Dashboard, somewhere else?

Your code DOES have an effect if that is somewhere in the node's runtime. It sets that property to true. But what is node.config used for? It has no meaning unless you consume it somewhere else. But certainly it does not have any impact on the Node-RED Editor since that is not how to communicate between the runtime and the Editor.

Sorry for not giving enough info here.

html

RED.nodes.registerType('notification',{
        category: 'Cumulocity',
        color: '#a6bbcf',
        defaults: {
            name: {value:""},
            active: {value: true},
            subscription: {
                value:"", required: true,
                validate: RED.validators.typedInput('subscriptionType')
            },
            subscriptionType: {
                value: "Select"
            },
            subscriber: {value:"", required: true},
            c8yconfig: {value:"", type:"c8yconfig", required: true},
            useenv: {value: false}
        },
        inputs: 0,
        icon: "file.png",
        outputs:1,
        label: function() {
            return this.name ||"notification-" + this.subscriber;
        },
        button: {
            toggle: "active",
            onclick: function() {
                var node = this;
                $.ajax({
                    url: 'notification/' + node.id +'/'+(node.active?"enable":"disable"),
                    cache: false,
                    dataType: 'json',
                    method: 'GET',
                    success: function (data) {
                        console.log("Success: " + JSON.stringify(data));
                    }
		        });
            }
        }
    });

The button is shown in the editor as on the inject or debug node and it works on click.

js

   function notificationNode(config) {
      RED.nodes.createNode(this, config);
      var node = this;
      node.config = config;
      node.config.active = true;

I thought assigning toggle: "active", would somehow bind that variable to the button. But I might have to look in the DOM to find out how to toggle by hand.

Thanks Marco

I may be wrong but I don't believe that the inject-style button was ever meant to have its status changed dynamically. Though certainly you would be able to manually disable it (e.g. change colour and prevent input) by using jQuery, however, there is limited interactivity available on the Editor. Really though, you are pushing the boundaries of the architecture there I think. You would probably need to mess with Node-RED's internal comms or events.

Much easier though would be to implement a "gate" function within the runtime and use the status message (available from the runtime) to indicate that the "gate" was open or closed. Simply don't let the injected message go anywhere if the gate is closed.

Yeah thanks, was having the same impression.... to stop messaging it seems more reasonable to deactivate the whole flow..

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