How do I fire a custom event from Polymer Dart?

后端 未结 3 1950
栀梦
栀梦 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:36

    I managed this using and the polymer helper method fire. This way you are able to listen to events fired from elements that are not children. source.

    todochange.html

    
    
    
      
      
    
    

    todochange.dart

    import 'package:polymer/polymer.dart';
    import 'dart:html';
    
    @CustomTag('todo-item')
    class TodoItemElement extends PolymerElement {
      @observable Item item;
    
      void change(Event e, var details, Node target) {
        // the name is the name of your custom event
        this.fire( "core-signal", detail: { "name": "todochange" } );
      }
    }
    

    Then any subscriber just has to do this

    subscriber.html

    ...
    
    ...
    
    

    subscriber.dart

    @CustomTag( "subscriber" )
    class Sub extends PolymerElement {
      ...
      void handleToDoChange( Event e, var detail, Node target ) {
        print( "Got event from " );
      }
      ...
    }
    

提交回复
热议问题