Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allouis/add-view-events
💥 Standalone event delegation for views 💥
https://github.com/allouis/add-view-events
Last synced: 24 days ago
JSON representation
💥 Standalone event delegation for views 💥
- Host: GitHub
- URL: https://github.com/allouis/add-view-events
- Owner: allouis
- Created: 2015-10-10T13:12:34.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-12T09:15:52.000Z (about 9 years ago)
- Last Synced: 2024-09-24T09:12:12.629Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 125 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# add-view-events
Adds delegated event listeners to an element, optionally binding the callbacks to a context, allows for easy removal of event listeners on cleanup
```
npm install add-view-events
```## Usage
```javascript
var addEvents = require('./add-view-events')function SomeView() {
this.el = document.createElement('div')
this.removeEvents = addEvents(this.el, {
'click button.some-class': this.shout
}, this)
}SomeView.prototype.shout = function() {
alert(this.msg)
}SomeView.prototype.remove = function() {
this.removeEvents()
this.el.remove()
}
```## API
#### `addEvents(el, events[, context])`
Attach event listeners and returns a cleanup function. `el` should be an element, `events` should be an object with `'event elem.selector'` keys and function properties.
The `context` argument is optional and will ensure callbacks are called with their context bound to `context`