How do I fire a custom event from Polymer Dart?

后端 未结 3 1937
栀梦
栀梦 2020-11-30 05:26

I want to fire/send/emit a custom event from inside a Polymer element. For example, I want to convert a normal DOM event like \"changed\" to a more semantic event like \"tod

3条回答
  •  自闭症患者
    2020-11-30 05:44

    Polymer has a helper method that simplifies firing events

    // dispatch a custom event
    this.fire('polymer-select', detail: {'item': item, 'isSelected': isSelected});
    

    Additional info:
    To make the event available to subscriber that want to add a listener programmatically

    // getter
    async.Stream get onPolymerSelect =>
        PolymerSelection._onPolymerSelect.forTarget(this);
    
    // private EventStreamProvider
    static const dom.EventStreamProvider _onPolymerSelect =
        const dom.EventStreamProvider('polymer-select');
    

    subscribe to the event programmatically instead of declaratively:

    ($['#ps'] as PolymerSelect) // get the children and cast it to its actual type
        .onPolymerSelect.listen((e) => print(e['isSelected'])); // subscribe
    

提交回复
热议问题