Frontend I can access methods of an instance of a class by static code:
instanceName.method(varValue);
The class is Gauge (library SVG Gauge) and with the above code I can control the pointer of my instanced gauge as expected. I can also control it with pre-render generated code in EJS:
<%= gaugeInstanceVar %>.setValue(<%= gaugeValueVar %>);
But I am getting a variable in through Server Sent Events after rendering and gaugeInstanceVar, containing the name of the instance used thus:
gaugeInstanceVar.method(varValue)
throws: Uncaught TypeError: X.setValue is not a function.
It also feels wrong to me, like failing to modify my variable, when I really want to use the value to call the instance.
What however would be the correct way of accessing the method (in my current case ".setValue()" of an active instance of a Class, using the variable gaugeInstanceVar?
Edit:
Using eval() would also achieve the desired effect
eval(sseTopic).setValue(sseValueNumber);
But I believe I should stay away from it and am still looking for the correct way of achieving the equivalent.
Edit 2 (feedback for Bergi)
I have condensed the code down to one if the 7 gauge types, here switch case 3.
The user input is just via form, everything apart from the code below is handled backend.
The library "SVG Gauges" takes the var to create the instance of Gauge. The divs with corresponding id's are created one step earlier - I don't think we need that code here.
EJS provides element.topic for the var, as well as the id, which is desired.
<%
data.forEach( element=> {
switch(element.gaugetype) {
case 'gauge3':
gaugeClass ='gauge-container three'; // used for div's
%>
<script>var <%= element.topic %> = Gauge(document.getElementById('<%= element.topic %>'), {
max: <%= element.value %>,
value: 50
} );</script>
<%
break;
default:
return;
break;
} %>
After removeSpecialCharacters(string) the variable sseTopic in the code below is identical with element.topic in the code above.
//Server Sent Event-Handler
let sse = new EventSource("http://localhost:3000/sse-stream");
sse.onmessage = function (event) {
let jdata = JSON.parse(event.data);
let sseValue = jdata.message;
let sseValueNumber = Number(sseValue);
let rawSseTopic = jdata.topicname;
let sseTopic = removeSpecialCharacters(rawSseTopic);
sseTopic.setValue(sseValueNumber); // Type ERROR not a function
// eval(sseTopic).setValue(sseValueNumber); not desirable b
};
function removeSpecialCharacters(string) {
let cleanString = string.replace(/,|\.|-|_|\|\s|\//g, "");
return cleanString;
}