Hello everyone,
I had time to continue working on my project for the last few weeks and I got a little confused watching the msg object in my ui node.
My ui node has one input and two outputs. It expects an incoming msg with a payload containing the data which then shows something to the user. First output returns the incoming (modified) msg and the second output returns true or false, depending on the state.
First question:
I want to have the same value after a page refresh so I set storeFrontEndInputAsState to true - which then updates the nodes msg object. Unfortunately the new msg object turns into an array of length two (because of the two outputs of my node I guess) so my $scope.watch has to look like this:
$scope.$watch('msg', function(msg) {
// msg from input
if (msg && msg.payload) {
$scope.timers = msg.payload;
// msg from F5
} else if (msg && msg[0] && msg[0].payload) {
$scope.msg = $scope.msg[0];
}
});
This solution works fine - but it is supposed to be like this?
Second question:
I want the incoming payload to be JSON data and parse/validate it in beforeEmit and stringify the data in beforeSend so that JSON leaves the node. After stringifying the data in beforeSend I realized that hitting F5 returns the msg object that was created after beforeSend. Therefore I changed §scope.watch to:
$scope.$watch('msg', function(msg) {
// msg after beforeEmit (parsedJSON) -> from input
if (msg && msg.payload) {
$scope.timers = msg.payload;
// msg from F5
} else if (msg && msg[0] && msg[0].payload) {
// page refresh (F5) sends the msg object that was created after beforeSend (JSON)
const data = angular.fromJson(msg[0].payload).timers;
$scope.msg[0].payload = data;
$scope.msg = $scope.msg[0];
}
});
This worked well too, until I noticed that switching tabs does not work anymore. Turned out switching tabs does not send the same msg as the input or page refresh, instead it sends the last known msg (I think) - which made me change the code again:
$scope.$watch('msg', function(msg) {
// msg after beforeEmit (noJSON) -> from input
if (msg && msg.payload) {
$scope.timers = msg.payload;
// msg turnes into an array after hitting F5 or switching tabs
} else if (msg && msg[0] && msg[0].payload) {
// page refresh (F5) sends the msg object that was created after beforeSend (JSON)
// switching tabs sends ?the last known msg object? (noJSON) -> so if parsing
// fails then it is already valid data
const data = angular.fromJson(msg[0].payload).timers;
if (data !== undefined) {
$scope.msg[0].payload = data;
}
$scope.msg = $scope.msg[0];
}
});
and again this works just like it should, but kind of feels inconsistent.
Wouldn't it be better if F5 and switching tabs would resend the same msg?