1

In this useful post we can learn how to listen to dispatched custom events by way of inline markup in the template: How do I fire a custom event from Polymer Dart? I wonder if it's possible to do the same in the dart-script when custom elements are dynamically created, like this:

var myElement = new Element.tag('my-element');
3

1 Answer 1

4

You need for each event handler in your component an EventStreamProvider (static)
and a getter for easy access.

static const EventStreamProvider<CustomEvent> logoutEvent = 
    const EventStreamProvider<CustomEvent>("my-logout");
Stream<CustomEvent> get onLogout =>  MyElement.logoutEvent.forTarget(this);

You fire an event like

this.dispatchEvent(new CustomEvent("my-logout", detail: {'someData': 'bla'}));

// Polymer provides a shortcut for this
fire('my-logout', detail: {'someData': 'bla'});

You subscribe to this event like

mySesson.onLogout.listen((e) => doSomething());
Sign up to request clarification or add additional context in comments.

2 Comments

Just a little note. I suppose 'mySesson' is a little copy/paste typo. It's supposed to be the dynamically created custom element.
I just used a name that fit with the example logout event. That is just the name of a variable referencing a component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.