I have a use-case where I need to get a Node's ID from inside OnEditPrepare in the Node's HTML frontend code.
I figured out a way to do it but it seems clunky and am wondering if there is something built-in I can access or is there a better way to do it.
My method was to create a small script to load into custom nodes:
/resources/MyCustomNode/GetNodeID.js
//
// Add this to any node where you want access to the NodeID
// from within the NodeRed HTML code like inside OnEditPrepare
//
// window.myNodeID
//
if (!window.myNodeID) {
window.myNodeID = null;
// Event listener for selection changes in the Node-RED workspace
RED.events.on('view:selection-changed', function(event) {
if (event.nodes && event.nodes.length > 0) {
window.myNodeID = event.nodes[0].id;
} else {
window.myNodeID = null;
}
});
}
And then load and access it in my Custom Node as such...
<script src="/resources/MyCustomNode/GetNodeID.js"></script>
<script type="text/javascript">
RED.nodes.registerType('MyCustomNode',{
category: 'SomeCategory',
color: '#FDD0A2',
inputs:1,
outputs:1,
icon: 'font-awesome/fa-arrow-right',
align: 'right',
label: ()=> { return this.nodeLabel||'MyCustomNode' },
oneditprepare: ()=>{
console.log('My Node ID: ', window.myNodeID);
}
}
});
I looked for a while but couldn't find anything.