Using the default node names feature in NR 3.0

I would like to use the default node naming process introduced in NR version 3.0 in one of my contributed nodes. I also want to leverage it to define another node property (outputTopic) in the code below.

onadd: function() {
    if (RED.settings.version.split('.')[0] >= 3 && this.name === "") {
        RED.actions.invoke("core:generate-node-names", this, {generateHistory: false})
        this.outputTopic = this.name
    }
}

This works perfectly when I drag an instance of the node from the palette, but if I use copy-and-paste to duplicate a node already in the workspace, the copy gets a new name, but it has the same outputTopic. I was surprised that copying worked at all, but is there something I can do get the other property too?

I think because the copy already has a name - copied from the original - so it isn't going into the if statement?

Of course. :slightly_smiling_face: That's right. But then the editor knows to rename the copy, and it must be tracking nodes of that type so it can keep on renaming them. I'll have to come up with another trick to cover the case.

The debug node does it slightly differently:

Yes, that was my starting point. Since debug is part of the core, I assumed (without checking) that this code was not present in previous versions of NR. Hence, my test for version number. The test for _DEFAULT_ works because that is how name is defined in the defaults object. I realize now that it works to determine whether the node instance has been dragged from the palette or pasted into the workspace. So, my requirements are: do no harm in earlier versions of NR; allow definition of properties in addition to name; and work properly for nodes pasted into the workspace. I have ideas, but I'm not quite there yet.

Why not use the same naming - make the default _DEFAULT_ and the node should do the right thing when it is added to a flow shouldn't it? All you need to do in the onAdd part is add the test for the Node-RED version along with testing for the default name. Or have I missed something obvious?

Just a couple of loose ends. Apparently, calling RED.actions.invoke does no damage in older versions of NR, so testing for the version number may not be necessary. I was not sure whether onadd is executed in the copy-and-paste case. Since it is, this.outputTopic = this.name can be moved outside the if, and everything works. If this survives a bit more testing, I'm done.

1 Like

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