Mithril.js: hyperscript delegated event handling question

Created on 5 Mar 2017  路  6Comments  路  Source: MithrilJS/mithril.js

Just a question on hyperscript event handling - did not find anything in the documentation so asking as an issue.

When you register a handler like this, does the listener get added directly to the dom element

m("button", {onclick: function() {count++}}, count + " clicks"),

Or does it end up as a "delegated event" similar to jQuery.on where there is a single listener for all descendent elements.

$( "#dataTable tbody" ).on( "click", "tr", function() {
});

Curious as to how many handlers get added when say we have a edit button on each row of a table with 1000 records.

Most helpful comment

I wrote delegated to handle event delegation because doing it manually was no fun.

All 6 comments

It's added directly to the element via addEventListener. So if you had a table of 1k records, each one with that button, you'd have 1k listeners (in which case I'd advise adding it to a parent element instead).

Thanks - I'd still be able to get the row that was clicked right?

Knockout has the dataFor(element) for getting the record that was clicked on.(http://knockoutjs.com/documentation/unobtrusive-event-handling.html)

Is there anything similar in Mithril? Or a recommended way to do this?

Thanks @pygy! Just what I was looking for, excited to try out Mithril for some projects...

I wrote delegated to handle event delegation because doing it manually was no fun.

@tivac I had completely forgotten about that lib... thanks for the reminder :-)

Was this page helpful?
0 / 5 - 0 ratings