summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <392513+tborychowski@users.noreply.github.com>2020-10-05 15:15:41 +0100
committerGitHub <noreply@github.com>2020-10-05 15:15:41 +0100
commit1a65698c413234c9a2c0b57f3459050ff4576c9a (patch)
tree5a1c93cad6042cd0b927153872bab1352e8121f2
parente81bd2d374d42ec2d3e39739cf7f36084ce7547e (diff)
fetch bookmarks every time; de-duplicate code
-rwxr-xr-xassets/js/data.js60
1 files changed, 13 insertions, 47 deletions
diff --git a/assets/js/data.js b/assets/js/data.js
index cf8b9c9..5f45926 100755
--- a/assets/js/data.js
+++ b/assets/js/data.js
@@ -1,50 +1,16 @@
-var data_links = "links.json";
-var bookmarks = JSON.parse(localStorage.getItem("links"));
-
-function handleLinks(data) {
- var mysource = document.getElementById("links-template").innerHTML;
- var mytemplate = Handlebars.compile(mysource);
- var myresult = mytemplate(data)
- document.getElementById("links").innerHTML = myresult;
+function fetchAndRender (name) {
+ fetch(name + '.json')
+ .then(response => response.json())
+ .then(data => {
+ const mysource = document.getElementById(name + '-template').innerHTML;
+ const mytemplate = Handlebars.compile(mysource);
+ const myresult = mytemplate(data);
+ document.getElementById(name).innerHTML = myresult;
+ });
}
-document.addEventListener("DOMContentLoaded", function () {
- if (!bookmarks) {
- fetch(data_links)
- .then(response => response.json())
- .then(function (data) {
- handleLinks(data);
- localStorage.setItem("links", JSON.stringify(data));
- });
- } else {
- handleLinks(bookmarks);
- }
-});
-
-var data_apps = "apps.json";
-
-document.addEventListener("DOMContentLoaded", function () {
- fetch(data_apps)
- .then( response => response.json())
- .then(
- function (data) {
- var mysource = document.getElementById("apps-template").innerHTML;
- var mytemplate = Handlebars.compile(mysource);
- var myresult = mytemplate(data)
- document.getElementById("apps").innerHTML = myresult;
- });
+document.addEventListener('DOMContentLoaded', () => {
+ fetchAndRender('apps');
+ fetchAndRender('links');
+ fetchAndRender('providers');
});
-
-var data_providers = "providers.json";
-
-document.addEventListener("DOMContentLoaded", function () {
- fetch(data_providers)
- .then( response => response.json())
- .then(
- function (data) {
- var mysource = document.getElementById("providers-template").innerHTML;
- var mytemplate = Handlebars.compile(mysource);
- var myresult = mytemplate(data)
- document.getElementById("providers").innerHTML = myresult;
- });
-}); \ No newline at end of file