Sulla base dei tuoi commenti, dovresti probabilmente fare ciò che fanno tutti gli altri HTMLElement con il caricamento delle risorse: fai in modo che il costruttore inizi un'azione di sideload, generando un evento di carico o errore a seconda del risultato.
Sì, questo significa usare le promesse, ma significa anche "fare le cose allo stesso modo di ogni altro elemento HTML", quindi sei in buona compagnia. Per esempio:
var img = new Image();
img.onload = function(evt) { ... }
img.addEventListener("load", evt => ... );
img.onerror = function(evt) { ... }
img.addEventListener("error", evt => ... );
img.src = "some url";
questo avvia un carico asincrono dell'asset di origine che, quando ha successo, finisce onload
e quando va storto finisce onerror
. Quindi, fai in modo che anche la tua classe faccia questo:
class EMailElement extends HTMLElement {
constructor() {
super();
this.uid = this.getAttribute('data-uid');
}
setAttribute(name, value) {
super.setAttribute(name, value);
if (name === 'data-uid') {
this.uid = value;
}
}
set uid(input) {
if (!input) return;
const uid = parseInt(input);
// don't fight the river, go with the flow
let getEmail = new Promise( (resolve, reject) => {
yourDataBase.getByUID(uid, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
// kick off the promise, which will be async all on its own
getEmail()
.then(result => {
this.renderLoaded(result.message);
})
.catch(error => {
this.renderError(error);
});
}
};
customElements.define('e-mail', EmailElement);
E poi fai in modo che le funzioni renderLoaded / renderError gestiscano le chiamate agli eventi e l'ombra dom:
renderLoaded(message) {
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<div class="email">A random email message has appeared. ${message}</div>
`;
// is there an ancient event listener?
if (this.onload) {
this.onload(...);
}
// there might be modern event listeners. dispatch an event.
this.dispatchEvent(new Event('load', ...));
}
renderFailed() {
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<div class="email">No email messages.</div>
`;
// is there an ancient event listener?
if (this.onload) {
this.onerror(...);
}
// there might be modern event listeners. dispatch an event.
this.dispatchEvent(new Event('error', ...));
}
Nota anche che ho cambiato il tuo id
in a class
, perché a meno che tu non scriva un codice strano per consentire una sola istanza del tuo <e-mail>
elemento su una pagina, non puoi usare un identificatore univoco e quindi assegnarlo a un gruppo di elementi.