I already accessed the msg from a template and it worked perfectly.
Now I need to generate multiple similar widgets, which should show data for different rooms.
They have:
- code for reacting on the msg and editing HTML values
- CSS style
- elements I editing
I tried just to copy the content of the template (all 3 sections mentioned above) to 3 different templates, but I had problems with accessing elements: CSS of every template unit sees all elements, because they are in the same document after deployment.
More structured solution would be to have one common template for css and scripts (styles are equal and I can access correct template unit from the script using the topic in my message).
I found special setting named "Template type" for it and put CSS and script into it:
<style>
.... my css ...
</style>
<script>
(function(scope) {
scope.$watch('msg', function(msg) {
if (!msg) return;
var room = msg.topic.split('.')[0];
var elements = document.getElementsByClassName(`room_state ${room}`);
if (elements.length() == 0) return; // searching for template unit for this room
var el = elements[0]; // must be only one
// here I change data in certain widget according to the message...
});
})(scope);
And I created several templates (with type "widget in group") with HTML data inside.
I connected msg to the template with the script and CSS (because the msg is being processed there I thought)
But when the first message arrives, I just lose my CSS. It looks like input at the template node of type "Added to site section" just replaces its content with the message.
What I should do? I could convert it to "widget in group" but I will see it as widget on the dashboard then. This solution looks better.
What am I doing wrong here?