From 0e6223da73b9f9c13d3faae9d5b4134fe1245a56 Mon Sep 17 00:00:00 2001 From: spacejack Date: Sat, 27 Oct 2018 16:41:21 -0400 Subject: [PATCH] Add docs about using EventListener objects (#2260) * Add docs about using EventListener objects * Fix typo * Less misleading description of event handlers * Fix a typo + grammar mistake * More grammar/typo fixes --- docs/hyperscript.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/hyperscript.md b/docs/hyperscript.md index d0ac27676..3d66c54b1 100644 --- a/docs/hyperscript.md +++ b/docs/hyperscript.md @@ -278,6 +278,29 @@ function doSomething(e) { m("div", {onclick: doSomething}) ``` +Mithril accepts functions and [EventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventListener) objects. So this will also work: + +```javascript +var clickListener = { + handleEvent: function(e) { + console.log(e) + } +} + +m("div", {onclick: clickListener}) +``` + +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-redraw specifically for a single event by setting `e.redraw = false` on it: + +```javascript +m("div", { + onclick: function(e) { + // Prevent auto-redraw + e.redraw = false + } +}) +``` + --- ### Properties