I have a class for reloading some elements.
const getMe = async () => await
fetchURL("https://MY.SITE/api/users/me")
.then((res) => res.data);
class loader {
EVERYonceWHENmethodCALLED() {
self.ME = (async () => await getMe())();
}
me() {
$("#me").html(`${self.ME.grade}${self.ME.number}[Tickets: ${self.ME.likeTicket}]`);
} // ** likeTicket depends on ME **
async chart() {
if ($("body").find("#chart").length) {
const musicChart = await fetchURL(`https://MY.SITE/api/music/chart?&gender=${self.ME.gender}`);
$("#chart").html(await listElementsOf(musicChart.data.list));
} // ** listElementsOf depends on ME ( It lets me know what musics I 'liked' to ) **
}
async search(q) {
if ($("body").find("#searchResult").length){
if(q) $("#searchResult").attr("data-q", q);
else q = $("#searchResult").attr("data-q");
const searchResult = await fetchURL(`https://MY.SITE/api/music/search?q=${q}`);
$("#searchResult").html(await listElementsOf(searchResult));
}
}
meAndChart() {
self.me();
self.chart();
}
loadEverything() {
self.meAndChart();
self.search();
}
}
I need to reload ME before reloading any other element, and I'm seeking a way to do this. I can employ new like new loader.me();, but I don't want to use new every time.
Is there any way for the intended job? Thanks for reading.
MEif it is not even used in you class? I can't make much sense of your code.elementsare DOM elements as you said, and I need to use ME, private user detail, to reload 'me' and 'chart' elements. I reload them by re-fetching data from the target site. ex)const musicChart = await fetchURL(`https://MY.SITE/api/music/chart?gender=${self.ME.gender}`;$("#chart").html(await listElementsOf(musicChart.data.list));- I used JQuery.fetchURLcalls into a central function and use only that function to fetch data. Then, in that function, you could loadMEonly if necessary.meandchartthat run without reloadingME, and public methods that callEVERYonceWHENmethodCALLEDbefore doing so.meAndCharwould callEVERYonceWHENmethodCALLEDfirst, and then the internal methods.