创建我自己的 iron-ajax 元素
Create my own iron-ajax element
我想创建我自己的 iron-ajax 元素,做 iron-ajax 做的事情,但多一点,在请求中添加一些 headers,目前我总是添加header 通过 data-binding 到 iron-ajax 的 headers 属性 但我不想在我构建的每个元素上编写代码,我想要 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>
我该如何处理 on-response?传递带有方法名称的 String 似乎不起作用。
我标记的答案是正确的。对于我编辑过的问题,我找到了解决方案:
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>
我想创建我自己的 iron-ajax 元素,做 iron-ajax 做的事情,但多一点,在请求中添加一些 headers,目前我总是添加header 通过 data-binding 到 iron-ajax 的 headers 属性 但我不想在我构建的每个元素上编写代码,我想要 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>
我该如何处理 on-response?传递带有方法名称的 String 似乎不起作用。
我标记的答案是正确的。对于我编辑过的问题,我找到了解决方案:
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>