Skip to content

Instantly share code, notes, and snippets.

@luruke
Created October 25, 2019 12:09
Show Gist options
  • Save luruke/44a01efd9442cd8bb138cc09547f6183 to your computer and use it in GitHub Desktop.
Save luruke/44a01efd9442cd8bb138cc09547f6183 to your computer and use it in GitHub Desktop.
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