UPDATE:响应和错误事件不再冒泡。https://github.com/PolymerElements/iron-ajax/releases/tag/v1.0.5真遗憾。
原题:
我想要创建基于iron的自定义ajax组件,以添加几个自定义头和处理程序。虽然还没有实现自定义元素继承,但我只是在我的-ajax中添加了iron,并且打算将所有api委托给iron,这在generateRequest中运行得很好。
但是,当它涉及到处理程序方法时,我注意到它在没有任何委托的情况下工作。在我的ajax中没有定义任何响应处理程序,但是handleResponse仍然被调用。
据我所知,之所以会出现这种情况,是因为Polymer.Base._addFeature._createEventHandler (polymer.html:345)使用“this”(顶级elt )作为处理程序方法定义的“主机”。
所以问题是:它是bug还是特性?
示例代码:
<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>
发布于 2015-06-23 17:58:59
大多数事件都是气泡,所以您只是通过放置在response实例上的处理程序从my-ajax中看到my-elt事件气泡到my-elt作用域。这在从较低作用域冒泡到上作用域的click事件中是相同的。
所以答案是:“特性”(指的是网络平台,而不是聚合物本身)。
https://stackoverflow.com/questions/30997153
复制相似问题