Custom Multiple Output Node

575 views
Skip to first unread message

Patrick Perte

unread,
Sep 26, 2017, 9:43:44 AM9/26/17
to Node-RED
Does node-red give us the ability to create a custom node and specify the number of default outputs we can have? I looked over the "Switch" node code and saw that it has one output as its default setting and the user can increase the number of outputs. Is there a way to set a default number of outputs when the custom node is added to the flow?

I've tried making the following changes so far to the code to accomplish my goal but nothing has worked:

Does node-red look for outputs in a different manner and format the node block accordingly?




----- After looking through the code one more time while I was writing this, I found:

Another piece of code that I've found that is common between the switch and function node (the only two that I can see that have multiple outputs as an option) is in the html file:

<input type="hidden" id="node-input-outputs"/>

The value of this hidden input element is changed with the oneditprepare function in each of the two default nodes I stated, and when the editor is closed the node adds its new outputs. Can this be moved somewhere so that as soon as the node is added to the flow, the two outputs are already present? 

Nick O'Leary

unread,
Sep 26, 2017, 9:55:19 AM9/26/17
to Node-RED Mailing List
The 'outputs' property at the top level of the node definition defines how many outputs it starts out with.

If you want to let the user to also be able to edit the number of outputs, you need to have an `outputs` property in the `defaults` array - but it must be a numeric value, not an object as you have it set to in the screenshot.

Here, for example is the Switch node's default value: https://github.com/node-red/node-red/blob/master/nodes/core/core/75-exec.html#L127

When you close the edit dialog, the editor checks to see if the node's outputs value has changed; if it has, it updates the appearance of the node to match.

The Function node has an input called node-input-outputs which the user can edit directly. Because of the naming convention, it just works.

The Switch node needs to set the number of outputs based on the number of rules. So its oneditsave function counts how many rules there are and updates the hidden field with the appropriate value that the editor uses to update the output count. In fact, it does a bit more than that.... because it handles reordered outputs as well as changes to how many there are. It does that by setting the node-input-outputs value as a JSON array in a special format we haven't documented. That tells the editor how the previous outputs map to its new outputs so it can maintain its wiring and keep the port labels properly lined up. If you don't need to worry about reordering outputs, then I won't go further.



Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/e08051c8-cc37-4b32-a389-8ab2d7485c21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Patrick Perte

unread,
Sep 26, 2017, 10:04:47 AM9/26/17
to Node-RED
Thanks for the quick reply. I simply changed the 'outputs' value and removed the one from the defaults and everything works as intended!
Reply all
Reply to author
Forward
0 new messages