Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs about using EventListener objects #2260

Merged
merged 5 commits into from
Oct 27, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/hyperscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ function doSomething(e) {
m("div", {onclick: doSomething})
```

Mithril adds event listeners to the DOM using [EventTarget#addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). This means you can provide a function (as above) or an [EventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventListener) object. For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not how it works internally, and I'd rather not give people a false impression on that. You could still mention that both functions and event listener objects work, just don't include the implementation detail that gives people the wrong impression, especially of performance implications. (Changing existing listeners is a simple object property write, not a removeEventListener + addEventListener pair.)


```javascript
var clickListener = {
handleEvent: function(e) {
console.log(e)
}
}

m("div", {onclick: clickListener})
```
dead-claudia marked this conversation as resolved.
Show resolved Hide resolved

By default, when an event attached with hyperscript fires, this will trigger Mithril's auto-redraw after your event callback returns (assuming you are using `m.mount` or `m.route` instead of `m.render` directly.) You can disable auto-rerdraw specifically for this event by adding a `redraw` property to the event with the value `false`:

```javascript
m("div", {
onclick: function(e) {
// Prevent auto-redraw
e.redraw = false
}
})
```

---

### Properties
Expand Down