Skip to content

Instantly share code, notes, and snippets.

@raggi

raggi/handler.js

Last active May 7, 2016
Embed
What would you like to do?
Modern JS event handling mixin
const Handler = Base => class extends Base {
handleEvent (evt) {
return this[`on${evt.type.replace(/^./, c => c.toUpperCase())}`](evt)
}
}
@raggi

This comment has been minimized.

Copy link
Owner Author

@raggi raggi commented May 7, 2016

Usage example:

class ExampleElement extends Handler(HTMLElement) {
  attachedCallback () {
    this.addEventListener('click', this)
  }
  detachedCallback () {
    this.removeEventListener('click', this)
  }
  onClick (evt) {
    console.log('clicked!')
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment