I am fairly new to es6, but want to recreate my jQuery code into clean es6 modules (one file per module) and drop jQuery at all. But I am already lost by binding a simple eventhandler to my buttons.
This is my currently button.js file:
import forEach from 'lodash/forEach';
export default class Buttons {
constructor(root) {
console.log('constructor hello');
this.root = document.body;
this.buttons = this.root.querySelectorAll('.default-button');
this.bind();
}
bind() {
console.log('bind hello');
forEach(this.buttons, (button) => {
button.addEventListener('click', () => {
console.log('Click event just for test purpose');
})
})
}
}
Here is my main.js file
class sitemodules {
constructor() {
sitemodules.domReady().then(this.ready.bind(this));
}
ready() {
this.initButtons();
}
initButtons() {
new Buttons();
}
static domReady() {
return new Promise((resolve) => {
document.addEventListener('DOMContentLoaded', resolve);
});
}
}
new sitemodules();
'constructor hello' and 'bind hello' are fired in the console, but I can't get the message I implemented with the click listener, thus the eventlistener seems not to be added properly.