From 54282c31bb5a515db07fab6df6c22eeace2f2057 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 17 Mar 2021 03:10:25 +0100 Subject: [PATCH] get rid of global variables because js is bad --- scripts/blog.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/scripts/blog.js b/scripts/blog.js index 58acca4..34e1872 100644 --- a/scripts/blog.js +++ b/scripts/blog.js @@ -4,19 +4,17 @@ $("#blog-archive-button").on("click", function () { $(this).html((archive.is(":visible") ? "Hide" : "Show") + " archived posts"); }); -let blogData; $.ajax({ dataType: "json", url: "blog/src/posts.json", cache: false, success: function (json) { - blogData = json; - populateBlog("All"); + populateBlog(json, "All"); forceToAnchor(); } }); -function populateBlog(cat) { +function populateBlog(json, cat) { let archive = $('#blog-archive'); let list = $('#blog-list'); let cats = $('#blog-cats'); @@ -24,10 +22,10 @@ function populateBlog(cat) { list.html(""); cats.html(""); - addCatButton(cats, "All"); - for (let i = blogData.length - 1; i >= 0; i--) { - var obj = blogData[i]; - addCatButton(cats, obj.cat); + addCatButton(json, cats, "All"); + for (let i = json.length - 1; i >= 0; i--) { + var obj = json[i]; + addCatButton(json, cats, obj.cat); if (cat == "All" || obj.cat == cat) { let p = ""; p += '
'; @@ -46,12 +44,12 @@ function populateBlog(cat) { } } -function addCatButton(cats, cat) { +function addCatButton(json, cats, cat) { let catAnchor = `blog-cat-${cat.toLowerCase().replace(" ", "_")}`; if (!$(`#${catAnchor}`).length) { cats.append(``); $(`#${catAnchor}`).on('click', function () { - populateBlog(cat); + populateBlog(json, cat); }); } } \ No newline at end of file