Reputation: 616
How would you translate this function from Javascript into Dart?
function addEvtListener(Element el, evtName, fn) {
return el.addEventListener ? el.addEventListener(evtName, fn, false) : el.attachEvent('on' + evtName, fn);
}
The main issue is having the event name dynamic. I obviously can call:
el.onClick.listen((event) => fn(event));
if the evtName
is a click event. But is there a method like this in Dart?
el.on("click").listen(event) => fn(event));
Upvotes: 2
Views: 1579
Reputation: 11171
The Events class documentation has an overview of the different ways to listen for an event by name: http://api.dartlang.org/docs/releases/latest/dart_html/Events.html
You'll see that you can do both:
new EventStreamProvider(evtName).forTaget(el).listen(fn);
or
el.on[evtName].listen(fn);
Upvotes: 3
Reputation: 76193
You can use :
(const EventStreamProvider<Event>(evtName)).forTarget(el).listen((e) => fn(e));
Upvotes: 2