Hi All
I am trying to make a node that has a config node and a drop down list of devices. The config node just contains the service, port and protocol of the server. The drop down list in the non-config node loads a list of devices from that server.
I had thought this would be a piece of cake as loads of nodes do similar things but I am having a few issues.
Issue number 1:
When I am initially setting up the first node and creating the config node I am unable to load the list of devices. The reason for this is when I make the async call from the edit dialogue to the node process the config node has not yet been saved so when I do
const configNode = module.nodes.getNode(req.params.configId)
in my node code that configID does not yet exist so nodejs does not have the server url to load nodes from.
In my editDialogue code I have:
$('#node-input-server').change(() => {
console.log(`CONFIG SERVER CHANGED: ${this.server}`);
const newConfigNode = RED.nodes.node(this.server);
console.log(newConfigNode);
})
but this.server
is undefined and there doesn't seem to be any way of getting the content of the config node to send it to the nodejs process to load the list of devices.
Issue number 2:
I have a validate function setup to ensure that a valid mac address is selected for the device:
defaults: {
name: {value:"soma-smart-shade"},
server: {type:"soma-smart-shade-config", required: true},
blindid: {validate: function(value) {
const regExp = /^(\w{2}:){5}\w{2}$/;
console.log(`VALIDATE: ${value} ${regExp.test(value)}`);
return regExp.test(value);
}}
},
this works fine. The issue is that when I open the edit dialog the value of my select box is initially "Loading..." as the list of devices is loaded. This obviously validates as false but when the list of devices is loaded and I select the currently selected device in the list the validation function is not called again so it remains in an invalid state. I have to select a different option to get the validation function to run again.
Here is my code:
$.getJSON(`soma-smart-shade-devices/${this.server}`, (data) => {
const blindIdInput = $('#node-input-blindid');
blindIdInput.prop('disabled', false);
blindIdInput.empty();
const defaultOption = new Option("Select blind...", "", true, true);
defaultOption.hidden = true;
defaultOption.disabled = true;
blindIdInput.append(defaultOption);
data.forEach(blind => {
blindIdInput.append(new Option(blind.name, blind.mac)); // , undefined, blind.mac === this.blindid
});
});
how do I force a re-validation of the select value after creating the options?
Thanks