我想创建自己的iron-ajax元素,它执行iron-ajax的功能,但多一点,向请求添加一些 header ,目前我总是通过数据绑定(bind)将 header 添加到headers属性中iron-ajax 但我不想在我构建的每个元素上都进行编码,我希望iron-ajax 元素自己执行此操作。
有没有办法扩展iron-ajax?
编辑:
<template>
<iron-ajax
auto
url="[[url]]"
handle-as="json"
debounce-duration="1000"
on-response=""
headers="[[headers]]"
></iron-ajax>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'em-ajax',
properties: {
headers: {
type: Object,
computed: 'computeHeaders(token)'
},
token: {
type: String,
notify: true,
value: "asdf"
},
url: {
type: String
}
},
computeHeaders: function() {
return {'Authorization' : 'Bearer {' + app.accessToken + '}'};
},
handleRequest: function(request) {
console.log("handling request", request);
request.header = this.computeHeaders();
}
});
})();
</script>
<em-ajax
url="http://api.asdf.safd.sdf/profile"
on-response="handleProfile"
></em-ajax>
我该如何处理回复?传递带有方法名称的字符串似乎不起作用。
我标记的答案是正确的。对于我编辑的问题,我找到了解决方案:
this.fire('em-response', {event: event, request: request});
我在自定义 ajax 元素中触发一个事件,并在使用自定义 ajax 元素的元素中添加了一个监听器。这是一个好的解决方案吗?
最佳答案
您可以嵌入<iron-ajax>
进入你自己的<fancy-ajax>
<fancy-ajax>
之间的组件和转发属性进出和<iron-ajax>
.
<dom-module id="fancy-ajax">
<template>
<iron-ajax ...></iron-ajax>
</template>
</dom-module>
关于javascript - 创建我自己的 Iron-ajax 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688100/