Skip to content

Instantly share code, notes, and snippets.

@PaquitoSoft
Last active September 29, 2017 23:35
Show Gist options
  • Save PaquitoSoft/c011e18a2925e03e2f8a9ab0382a920a to your computer and use it in GitHub Desktop.
Save PaquitoSoft/c011e18a2925e03e2f8a9ab0382a920a to your computer and use it in GitHub Desktop.
class BasePage {
constructor(data = {}) {
this.state = data;
// Create the main HTMLElement for this page so we can
// attach DOMEvent listeners to it
this.$el = document.createElement('div');
Object.keys(this.events || {}).forEach(key => {
const [eventName, selector] = key.split('|');
this.$el.addEventListener(eventName, (function(selector, event) {
if (event.target.matches(selector)) {
this.events[key].call(this, event);
}
}).bind(this, selector));
});
}
updateState(newState) {
this.state = Object.assign({}, this.state, newState);
this.render();
}
render() {
this.$el.innerHTML = this.html();
return this.$el;
}
}
export default BasePage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment