Skip to content

Instantly share code, notes, and snippets.

@luruke
Created Oct 25, 2019
Embed
What would you like to do?
import Button from './button/button';
import Person from './person/person';
import Footer from './footer/footer';
const elements = {
button: Button,
person: Person,
footer: Footer,
// ...
};
class Dom {
constructor() {
this.id = 0;
this.instances = {};
}
register(el) {
const id = `id_${this.id++}`;
el.dataset.glid = id;
const type = el.dataset.type || 'button';
const instance = new elements[type](el);
this.instances[id] = instance;
el.classList.add('gl');
}
getInstanceFromEl(el) {
return this.instances[el.dataset.glid];
}
unregister(el) {
const instance = this.getInstanceFromEl(el);
el.classList.remove('gl');
instance.destroy();
}
}
export default new Dom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment