Polymer 1.x: Imperatively adding an event listener

I added an event listener to my user element for my call to <iron-ajax> .

Question

Is there a shorter (more convenient syntax) way to strongly (i.e. using Javascript) add an event listener to Polymer?

In other words, does the Polymer library contain any syntactic sugar for this?

order element.html
 <template> ... <iron-ajax id="ajax" last-response="{{ajax}}"></iron-ajax> ... <template> <script> ... var that = this, t = this.$.ajax; t.addEventListener('response', function(e) { console.log(that.ajax); }); ... </script> 

Study

The documentation here says:

You can also add an event listener to any element of the this.$ Collection using nodeId.eventName syntax.

But I think this only applies when using the listeners property in a Polymer object, as in:

 listeners: { 'tap': 'regularTap', 'special.tap': 'specialTap' }, 
+5
source share
1 answer

What should work in JS (Dart only)

  this.listen(this.$.ajax, 'last-response', 'lastResponseHandler'); 

there is also this.unlisten() to unsubscribe from events. I assume that if you add it imperatively, you also need to fix it to prevent memory leak.

Literature:

+4
source

Source: https://habr.com/ru/post/1239399/


All Articles