更新:响应和错误事件不再起泡。 https://github.com/PolymerElements/iron-ajax/releases/tag/v1.0.5 太遗憾了。
原始问题:
我想基于iron-ajax创建自定义ajax组件,以添加几个自定义标头和处理程序。虽然尚未实现自定义元素继承,但我刚刚将my-ajax添加到my-ajax并将所有api委托给iron-ajax,这对generateRequest工作正常。
但是当它得到处理程序方法时,我注意到它没有任何委托就可以工作。在my-ajax elt中没有定义响应处理程序,但仍然调用handleResponse。
据我了解,这是因为Polymer.Base._addFeature._createEventHandler(polymer.html:345) 使用'这个',这是顶级elt,作为' host'用于处理程序方法定义。
所以问题是:它是错误还是功能?
示例代码:
<link rel="import" href="https://raw.githubusercontent.com/Polymer/polymer/master/polymer.html">
<link rel="import" href="https://raw.githubusercontent.com/PolymerElements/iron-ajax/master/iron-ajax.html">
<dom-module id="my-ajax">
<template>
<iron-ajax
id="ironAjax"
url="http://echo.jsontest.com/key/value/otherkey/othervalue"
handle-as="json"
debounce-duration="300"
>
</iron-ajax>
</template>
<script>
Polymer({
is: "my-ajax",
generateRequest: function(){
this.$.ironAjax.generateRequest();
}
});
</script>
</dom-module>
<dom-module id="my-elt">
<template>
<button on-click="buttonClick">Button</button>
<my-ajax
id="myAjax"
on-response="handleResponse">
</my-ajax>
</template>
<script>
Polymer({
is: "my-elt",
buttonClick: function(){
this.$.myAjax.generateRequest();
},
handleResponse: function(event) {
alert('got response');
}
});
</script>
</dom-module>
<my-elt></my-elt>
&#13;
答案 0 :(得分:0)
大多数事件都会冒泡,因此您只是通过放置在response
实例上的处理程序看到my-ajax
事件从my-elt
冒出my-ajax
范围。这与从较低范围冒泡到较高范围的click
事件相同。
答案是:&#34;功能&#34; (网络平台,不仅仅是聚合物本身)。