Global set/get in dashboard template node

951 views
Skip to first unread message

Yonatan Gatica

unread,
May 25, 2017, 7:54:31 PM5/25/17
to Node-RED
Is there a way to access the global context from the script tag in the template node for dashboard?

IE:

<md-button class="md-raised" ng-click="setGlobal()">Set Global</md-button>

<script>
    (function(scope) {

        scope.setGlobal=function(){
            global.set("TestSet",Test);    //this can't reach global.
        }

    })(scope);
</script>

Yonatan

steve rickus

unread,
May 25, 2017, 9:05:16 PM5/25/17
to Node-RED
I just did something like this yesterday -- while you cannot set the globalContext directly (afaik), you can send the value back from the ui_template node. Wire the output to a change node which copies the msg.payload to global et voila! Make sure to uncheck the "Pass through messages from input" option, unless you want to validate the contents of every msg coming out of the ui_template node.

Your code is very close to what I did -- however, i was also passing a value to the function. In your case I don't think you need to dynamically assign the value that goes into the global context, so you can just put it directly into the msg to be returned, like so:

<md-button class="md-raised" ng-click="setGlobal()">Set Global</md-button>

<script>
   
(function(scope) {
        scope
.setGlobal = function() {

            scope
.send({ topic: "SetGlobal", payload: "Test" }); //this is the output msg
       
};
   
})(scope);
</script>

I have not tested this code, but it should be close...
--
Steve

Julian Knight

unread,
May 26, 2017, 4:03:00 AM5/26/17
to Node-RED
The global/flow/context variables exist in the server back-end, not in the Dashboard front-end.

The easy way is to add a change node and add the variable to a safe place on the msg. Then you can pick it up in the Dashboard.
Reply all
Reply to author
Forward
0 new messages