Skip to content

Commit

Permalink
make the pages in spa.json file order unnecessary
Browse files Browse the repository at this point in the history
by making loop and checking if "SPAname" in page object "in spa.json" is same to html element "SPAname" meta tag
  • Loading branch information
HamzaElkotp committed Sep 21, 2022
1 parent 97ee99b commit 440cf26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
15 changes: 13 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ function jsonRead(jsonpages, metatagArr){
}).then((data)=>{
function evenAdder(){

// to make No need to stick to static "page title" like: "SPAtitle", "page url" like: "SPAurl" or "page name" like: "SPAname", in your json file, name them as you want but be carefull to their order
let SPAname = metatagArr[0];
let SPAurl = metatagArr[1];
let SPAtitle = metatagArr[2];

let elements = document.querySelectorAll('[SPAname]');

// Loop through "SPAname" meta tag elemnts to add to them 'click' event
elements.forEach((ele, index)=>{
ele.addEventListener('click',()=>{
document.title = data[index][SPAtitle];
window.history.pushState("","",data[index][SPAurl]);

// change title, url regardless of pages or json file objects order
data.forEach((item, dataI)=>{
if(data[dataI][SPAname] == ele.getAttribute("SPAname")){
document.title = data[dataI][SPAtitle];
window.history.pushState("","",data[dataI][SPAurl]);
}
})


})
})
}
Expand Down
17 changes: 9 additions & 8 deletions spa.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
[
{
"SPAnameR" : "home",
"SPAurlR": "../",
"SPAtitleR": "SPA APP | My Logic",
"Body": {

}
},
{
"SPAnameR" : "customers",
"SPAurlR": "customers",
Expand All @@ -23,6 +15,15 @@

}
},
{
"SPAnameR" : "home",
"SPAurlR": "../",
"SPAtitleR": "SPA APP | My Logic",
"Body": {

}
},

{
"SPAnameR" : "notifications",
"SPAurlR": "notifications",
Expand Down

0 comments on commit 440cf26

Please sign in to comment.