When writing client side static JS functions, what is the proper way to define and then call them so they are specific to the node I am creating?
Are we supposed to declare functions within the registerType call? If so, how do you call the function?
RED.nodes.registerType(‘my-custom-node’,{
oneditprepare: function(){
console.log("prepare this node");
renderTable('table-container-id'); // <- where to define and how to call my own function?
},
renderTable: function(tableContainerId){
// render a table
}
}
Further, how would I re-use renderTable in all of my package's nodes?
if I define my function outside the registerType call, I end up with a global function which could obviously create a collision.
If all of your nodes are defined in the same file, then you can wrap all of your code in a self-executing function to keep the scope private to the file:
In other words, everything inside that enclosing function can see renderTable, but nothing outside of it can.
If you nodes are split across multiple files, it gets a bit more involved.
Previously, we've suggested putting them in your own separate .js file and then use a custom admin end point to serve up the file and a <script> tag in your node's html to load it in. That has never been an entirely satisfactory answer as it involves too many pieces for what ought to be a relatively straight forward task. Plus it is still loading the code into the global namespace.
Another option that I hadn't considered before is what you propose in your question - essentially adding the function to one of your node's type definitions. You can then access that from anywhere else with:
The final option is, as you say, just declare a global function that is visible to everything and pick a name that is unlikely to cause a clash - for example, prefix it with your node-type in some format.
I'm trying the getType approach. I'm defining all of my functions under my highest level config node and then setting a reference to it with getType anywhere I need to use those functions.
I can't remember what defines an entry in the pallet. Is it the .html file or the .js file or a combination of those and the package.json entry?
I'm just adding new properties to the node's registration. Those properties aren't used by NR to render the node, so the pallet shouldn't be affected. But those properties are accessible from any other node's HTML file by using the getType method, so it makes for a convenient place to define and namespace all of my functions.
Some of the options in the registerType function impact the pallet - like I said, I can't remember what impact that might have. I assume none since you aren't likely to set them but I don't know for sure which is why I asked Nick.