janat08
janat08

Reputation: 1787

Event capture on mithril?

is there no event capturing? Id like to force event handling order, in autocomplete component, onblur event fires before onclick on item in a list, causing the list to toggle twice and so remaining visible after flickering.

Upvotes: 3

Views: 1084

Answers (2)

Barney
Barney

Reputation: 16456

Event binding in Mithril is a very simple convenience feature that stands in for binding an event using standard non-capturing addEventListener, and automatically queues a redraw on resolution (this is in contrast to other view libraries like React, where event binding is an elaborate, opinionated system of its own).

When you want to do things differently, Mithril makes it very easy to break into direct DOM access by allowing you access to lifecycle methods inline. Specifically, the oncreate method will let you bind events however you choose.

m('div', {
  // this
  onclick : e => {},
  // is the same as this
  oncreate: vnode => {
    vnode.dom.addEventListener('click', e => {
      m.redraw()
    })
  },
})

Be warned that the click event will always resolve after blur, even if the former is capturing and the latter is bubbling - you may need to use mousedown instead. Here's a demo showing how to bind capturing events and logging the sequence of different events.

Upvotes: 6

janat08
janat08

Reputation: 1787

Apparently capturing isn't supported, despite google showing m.capture method from years back. You just register eventListener with true param for it having to be captured, and I suppose call m.redraw() at the end of the handler.

Upvotes: 1

Related Questions