Can I pass not only String attributes but also object references to an event handler?
Sample gwt_contacts from https://github.com/sethladd/dart-polymer-dart-examples:
contacts_view.html:
<div id="contact-list">
<template repeat="{{contacts}}">
<div>
<input type="checkbox">
<span on-click="selectContact" data-id="{{id}}">{{name}}</span>
</div>
</template>
</div>
handler in contacts_view.dart
void selectContact(MouseEvent event, var detail, SpanElement target) {
String id = target.attributes["data-id"];
selectedContact = contacts.firstWhere((Contact contact) => contact.id == id);
}
How can I pass the contact reference instead of the ID?
similar question: How do I pass arbitrary data to a click event handler function from a Dart polymer web component
CHANGED:
With the hint of Christophe Herreman I added a new element:
html:
<polymer-element name="contact-view-item">
<template>
<span on-click="select">{{contact.name}}</span>
</template>
</polymer-element>
dart:
@CustomTag("contact-view-item")
class ContactViewItem extends PolymerElement {
@observable @published Contact contact;
void select(MouseEvent event, var detail, var target) {
dispatchEvent(new CustomEvent("contactselected", detail: contact));
}
}
and changed the list to:
<div id="contact-list">
<template repeat="{{contact in contacts}}">
<div>
<input type="checkbox">
<contact-view-item contact="{{contact}}" on-contactselected="contactSelectedHandler"></contact-view-item>
</div>
</template>
</div>
and added the handler to ContactsView:
void contactSelectedHandler(CustomEvent event) {
selectedContact = event.detail;
}
But dispatching the custom event will throw an error:
Uncaught Error: unsupported object type for conversion
Exception: unsupported object type for conversion
undefined (undefined:0:0)
The connection seems to be fine, because if I don't pass contact as detail than contactSelectedHandler will be called, but detail is - of course - null.