Everything is functioning as expected, with the event listener successfully capturing the custom event when it is dispatched from the window and listened for as loading
, all seems to be working well.
const MyLib = mylib();
function mylib() {
const res = {
init: (data) => {
let loading = new CustomEvent('loading', {detail: { loading: true }});
window.dispatchEvent(loading);
}
}
return res;
}
event listener
window.addEventListener('loading', handleLoading);
I'm looking to update it to use MyLib.addEventListener
instead of window.addEventListener
.
Additionally,
window.dispatchEvent(loading);
should become MyLib.dispatchEvent(loading);
However, when attempting this change, I encounter an error message stating
TypeError: MyLib.addEventListener is not a function
The solution posted below involves using a class, but I am curious if it is possible to achieve this without utilizing a class structure.