dart-archive / polymer-dart

Polymer support for Dart
https://pub.dartlang.org/packages/polymer
BSD 3-Clause "New" or "Revised" License
181 stars 33 forks source link

can't read event properties on listener #683

Closed terrywarwar closed 8 years ago

terrywarwar commented 8 years ago

I have a global listener

 document.addEventListener("add-to-alert",(e){
      print(e.detail);
     }) ;

And then I fire an event:

 fire('add-to-alert', detail: {'id': id, 'code':code});

I can no longer read the detail property and I get the following error

Class 'Event' has no instance getter 'detail'

NoSuchMethodError: method not found: 'detail'
Receiver: Instance of 'Event'

It works when I use convertToDart(e.detail). Is that the correct method? It was working before without it.

jakemac53 commented 8 years ago

This is mentioned in the changelog for 1.0.0-rc.12, and is unfortunately working as intended. In polymer JS they now just add a detail field to regular event objects, instead of using a real CustomEvent. Afaik this is a performance thing, as it allows them to re-use event objects and just change the detail field.

This is primarily an issue when using the regular dom apis to listen for events, the polymer apis (@Listen, etc) will automatically convert it for you. The simplest solution is something like this:

document.addEventListener("add-to-alert",(e){
  e = convertToDart(e);
  print(e.detail);
}) ;