From 45711e18e70b9765416330e2a26b75d4e5225782 Mon Sep 17 00:00:00 2001 From: Kristjan Komlosi Date: Sat, 2 Nov 2019 11:59:06 +0100 Subject: [PATCH] table fill rewrite, no globalObject necessary --- frontend/frontend.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/frontend/frontend.js b/frontend/frontend.js index 81d3163..e42b100 100644 --- a/frontend/frontend.js +++ b/frontend/frontend.js @@ -1,5 +1,4 @@ // All code in this file is licensed under the ISC license, provided in LICENSE.txt -var globalObject; $('#update').click(function () { updateData(); }); @@ -9,12 +8,15 @@ function updateData () { const url = 'http://' + window.location.hostname + ':5000/data'; $.ajax({ // spawn an AJAX request url: url, - success: function (data, status) { globalObject = data; console.log(data); graphSpectralData(globalObject[0], 0); }, + success: function (data, status) { + console.log(data); + graphSpectralData(data[0], 0); + fillTableData(data); + }, timeout: 2500 // this should be a pretty sane timeout - }) + }); } - function graphSpectralData (obj, dom) { // graph spectral data in obj into dom var graphPoints = []; @@ -33,14 +35,17 @@ function graphSpectralData (obj, dom) { yaxis: { color: 'blue' } - } + }; $.plot('#graph', [graphPoints], options); // flot expects an array of arrays (lines) of 2-element arrays (points) } -function fillLuxUv (obj, dom) { - $(dom).find('#lx').text(obj[1]); - $(dom).find('#uva').text(obj[2][0]); - $(dom).find('#uvb').text(obj[2][1]); - $(dom).find('#uvi').text(obj[2][2]); +function fillTableData (obj) { + // fill the obj data into HTML tables + Object.keys(obj[0]) + .forEach((element) => { $('#' + element).text(obj[0][element]); }); + $('#lx').text(obj[1]); + $('#uva').text(obj[2][0]); + $('#uvb').text(obj[2][1]); + $('#uvi').text(obj[2][2]); }