From df5aa476dd01f6e278dbcef1968e50ef2039ed88 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Mon, 2 May 2016 20:35:33 +0200 Subject: [PATCH] (dashboard) plugin new style ajax poller use data-plugin to define the data collector (server side api/plugins/ plugin) and data-callback to define the javascript function to call after receiving data. The callback uses 2 parameters (sender, remote data), where sender is the dom attribute on which the data-plugin was defined --- src/www/index.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/www/index.php b/src/www/index.php index fb0578d3a..eef646cef 100644 --- a/src/www/index.php +++ b/src/www/index.php @@ -207,6 +207,39 @@ include("fbegin.inc");?> $("#iform").submit(); return false; } + + /** + * ajax update widget data, searches data-plugin attributes and use function in data-callback to update widget + */ + function process_widget_data() + { + var plugins = []; + var callbacks = []; + // collect plugins and callbacks + $("[data-plugin]").each(function(){ + if (plugins.indexOf($(this).data('plugin')) < 0) { + plugins.push($(this).data('plugin')); + } + if ($(this).data('callback') != undefined) { + callbacks.push({'function' : $(this).data('callback'), 'plugin': $(this).data('plugin'), 'sender': $(this)}); + } + }) + // collect data for provided plugins + $.ajax("/widgets/api/get.php",{type: 'get', cache: false, dataType: "json", data: {'load': plugins.join(',')}}) + .done(function(response) { + callbacks.map( function(callback) { + try { + if (response['data'][callback['plugin']] != undefined) { + window[callback['function']](callback['sender'], response['data'][callback['plugin']]); + } + } catch (err) { + console.log(err); + } + }); + // schedule next update + setTimeout('process_widget_data()', 30000); + }); + }