Polymer 1.x: 强制添加事件监听器

Polymer 1.x: Imperatively adding event listener

我已经为我的 <iron-ajax> 调用添加了一个事件侦听器到我的自定义元素。

问题

Is there a shorter (more convenient syntax) way to imperatively (i.e., using Javascript) add the event listener in Polymer?

换句话说,Polymer 库是否包含任何语法糖?

自定义-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>

研究

The documentation here says:

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

但我认为这仅适用于在 Polymer 对象中使用 listeners 属性 时,如:

listeners: {
  'tap': 'regularTap',
  'special.tap': 'specialTap'
},

什么也应该在 JS 中工作(仅在 Dart 中尝试过)

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

还有this.unlisten()取消活动订阅。 我假设如果你命令式添加它,你也需要命令式地删除它以防止内存泄漏。

参考文献: