resolve-handlers
v0.4.1
Published
Resolve handlers from strings.
Downloads
4
Maintainers
Readme
Resolve handlers ·
Resolve handlers using strings. Inspired by event delegation in Backbone.js.
Installation
npm install resolve-handlers
Example usage
import { resolveDOMEventHandlers } from 'resolve-handlers';
/**
* CommentController
*/
export default class CommentController {
constructor(element) {
// Target for event listeners
this.element = element;
// Resolve event handlers
this._resolvedEventHandlers = resolveDOMEventHandlers.call(this, this.getEventHandlerStrings());
// Add event listeners
this.addEventListeners();
}
getEventHandlerStrings() {
return [
'click .comment > .btn-delete: onDeleteCommentClick',
'focusin .comment: onCommentFocus',
'click .comment: onCommentClick'
];
}
onDeleteCommentClick() {
/* delete handler */
}
onCommentFocus() {
/* focus handler */
}
onCommentClick() {
/* click handler */
}
addEventListeners() {
this._resolvedEventHandlers.forEach(eventHandler => {
this.element.addEventListener(eventHandler.eventType, eventHandler);
});
}
removeEventListeners() {
this._resolvedEventHandlers.forEach(eventHandler => {
this.element.removeEventListener(eventHandler.eventType, eventHandler);
});
}
}
Change log
0.4.0
- Dependency updates
- Babel config for build
0.3.0
- getResolveFunction now returns a function that needs context.
- resolveDOMEventHandlers tests.
- Dev env config and dependency updates (eslint, babel, nyc).
0.2.0
- Removed dependency component-closest
License
This software is licensed under the MIT License.