Skip to content

Commit

Permalink
Update JS to cleanup and handle multiple identical requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pingevt committed Apr 13, 2024
1 parent 6b564db commit 3d72131
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions assets/src/js/content-ajaxing.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,7 @@
}

if (elem.dataset.ajaxCommands) {
// fetch(elem.dataset.ajaxCommands)
// .then((response) => {
// // console.log(response, response.text(), response.status, response.statusText);
// return response.text();
// })
// .then((text) => {

// console.log(text);

// elem.innerHTML = text;
// elem.classList.add("loaded");

// Drupal.attachBehaviors(elem);

// if (elem.dataset.ajaxEvent) {
// const timelineDataEvent = new Event(elem.dataset.ajaxEvent);
// window.dispatchEvent(timelineDataEvent);
// }
// });

Drupal.ajax({url: elem.dataset.ajaxCommands, httpMethod: 'GET'}).execute();
Drupal.ajax({ url: elem.dataset.ajaxCommands, httpMethod: 'GET' }).execute();
}

contentObserver.unobserve(elem);
Expand Down Expand Up @@ -81,15 +61,25 @@
// Load Ajaxable content.
const ajaxElsNow = document.querySelectorAll("[data-ajax-now]");

// Data obj here so we don't make multiple calls if we don't need to.
let data = {};
ajaxElsNow.forEach(element => {
fetch(element.dataset.ajaxNow)
if (!data[element.dataset.ajaxNow]) {
data[element.dataset.ajaxNow] = [];
}
data[element.dataset.ajaxNow].push(element);
});
for (const [url, elements] of Object.entries(data)) {
fetch(url)
.then((response) => response.text())
.then((text) => {
element.innerHTML = text;
element.removeAttribute("data-ajax-now");
element.classList.add("ajax-now--loaded");
elements.forEach(element => {
element.innerHTML = text;
element.removeAttribute("data-ajax-now");
element.classList.add("ajax-now--loaded");
});
});
});
};
},
};

Expand Down

0 comments on commit 3d72131

Please sign in to comment.