You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
6 years ago
|
// -- Functions --
|
||
|
|
||
|
/**
|
||
|
* binds the data for each element
|
||
|
*/
|
||
|
function bindData(){
|
||
|
let mods = $('model');
|
||
|
mods.each(function(){
|
||
|
let model = $(this);
|
||
|
if (model.attr('refresh-interval') != undefined) {
|
||
|
bindModelData(model);
|
||
|
setInterval(()=> { bindModelData(model) }, model.attr('refresh-interval'));
|
||
|
}
|
||
|
else bindModelData(model);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function bindModelData(model) {
|
||
|
$.get(model.attr('src'), function(data){
|
||
|
$('*[model='+model.attr('name')+']').each(function() {
|
||
|
let elem = $(this);
|
||
|
elem.html(getDataByPath(JSON.parse(data), elem.attr('datapath')));
|
||
|
})
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the data of an array by accessing the given path-string
|
||
|
* @param {JSON-Object} array Contains the data.
|
||
|
* @param {String} path The path to the wanted data.
|
||
|
* @return {Object} Returns anything that the path points to.
|
||
|
*/
|
||
|
function getDataByPath(array, path) {
|
||
|
let data = array;
|
||
|
path.split('.').forEach(function(p){
|
||
|
data = data[p];
|
||
|
});
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
// -- Events --
|
||
|
// Define what happens onLoad
|
||
|
$(document).ready(function(){
|
||
|
bindData();
|
||
|
});
|