Created
October 25, 2019 12:09
-
-
Save luruke/44a01efd9442cd8bb138cc09547f6183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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