iron-ajax (Polymer 1.0) 响应事件触发两次
iron-ajax (Polymer 1.0) on-response event firing twice
有没有人遇到过 iron-ajax 的响应事件为单个请求触发两次?我已经仔细检查过,实际上我只提交了一个请求。这是我的 iron-ajax 实现(只是一个包装 iron-ajax 的元素):
<dom-module id="my-ajax">
<template>
<iron-ajax id="ajax" auto="{{auto}}" url="{{url}}" method="{{method}}" headers="{{headers}}" body="{{body}}" handle-as="json" content-type="application/json" on-response="responseHandler" on-error="errorHandler" with-credentials></iron-ajax>
</template>
</dom-module>
// Register the polymer element
Polymer({
is: 'my-ajax',
properties: {
actionDesc: {type: String, value: ""},
auto: {type: Boolean, value: false},
body: {type: String, value: null},
headers: {type: Object, value: null},
isBusy: {
// One-way binding setup (i.e. child to host only)
type: Boolean,
value: false,
readOnly: true,
notify: true
},
method: {type: String, value: null},
user: {type: Object, value: null},
url: {type: String, value: null}
},
generateRequest: function() {
if (!this.isBusy) {
// Execute request as it isn't currently busy processing a previous request
this.isBusy = true;
this.$.ajax.generateRequest();
} else {
// TODO: Queue up this request
}
},
responseHandler: function(e, detail) {
console.log(this.id + " responseHandler fired!\n");
this.isBusy = false;
this.fire("handle-response", detail.xhr.response);
}
});
答案:
我认为您应该从 <iron-ajax>
声明中删除 auto
属性及其绑定,如下所示:
<iron-ajax id="ajax" url="{{url}}" method="{{method}}"
headers="{{headers}}" body="{{body}}" handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler" with-credentials></iron-ajax>
我认为问题出在“auto
”属性,Polymer´s documentation 显示了这样的示例:
<iron-ajax
auto
url="http://gdata.youtube.com/feeds/api/videos/"
params='{"alt":"json", "q":"chrome"}'
handle-as="json"
on-response="handleResponse"
debounce-duration="300"></iron-ajax>
文档说:
auto
{Boolean
} default: false
If true, automatically performs an Ajax request when either url or params changes.
所以,我认为当你添加“auto
”属性时,它的值默认自动设置为真,即使你绑定了它。这就是为什么我认为你应该删除它。
对不起我的英语,希望你能理解我。
如 所述,如果 auto
属性为真,则 iron-ajax
将在 url[ 时自动执行请求=28=] 或 params 更改。
为了使绑定按您希望的方式工作而无需调用两次,您的绑定应该在 auto
属性的 =
之前有 $
,如下所示:
<iron-ajax
id="ajax"
auto$="{{auto}}"
url="{{url}}"
method="{{method}}"
headers="{{headers}}"
body="{{body}}"
handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler"
with-credentials>
</iron-ajax>
从Polymer docs开始,像这样绑定的自动属性只有在绑定值为真时才会被设置
聚会有点晚了,但希望这对某人有所帮助
有没有人遇到过 iron-ajax 的响应事件为单个请求触发两次?我已经仔细检查过,实际上我只提交了一个请求。这是我的 iron-ajax 实现(只是一个包装 iron-ajax 的元素):
<dom-module id="my-ajax">
<template>
<iron-ajax id="ajax" auto="{{auto}}" url="{{url}}" method="{{method}}" headers="{{headers}}" body="{{body}}" handle-as="json" content-type="application/json" on-response="responseHandler" on-error="errorHandler" with-credentials></iron-ajax>
</template>
</dom-module>
// Register the polymer element
Polymer({
is: 'my-ajax',
properties: {
actionDesc: {type: String, value: ""},
auto: {type: Boolean, value: false},
body: {type: String, value: null},
headers: {type: Object, value: null},
isBusy: {
// One-way binding setup (i.e. child to host only)
type: Boolean,
value: false,
readOnly: true,
notify: true
},
method: {type: String, value: null},
user: {type: Object, value: null},
url: {type: String, value: null}
},
generateRequest: function() {
if (!this.isBusy) {
// Execute request as it isn't currently busy processing a previous request
this.isBusy = true;
this.$.ajax.generateRequest();
} else {
// TODO: Queue up this request
}
},
responseHandler: function(e, detail) {
console.log(this.id + " responseHandler fired!\n");
this.isBusy = false;
this.fire("handle-response", detail.xhr.response);
}
});
答案:
我认为您应该从 <iron-ajax>
声明中删除 auto
属性及其绑定,如下所示:
<iron-ajax id="ajax" url="{{url}}" method="{{method}}"
headers="{{headers}}" body="{{body}}" handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler" with-credentials></iron-ajax>
我认为问题出在“
auto
”属性,Polymer´s documentation 显示了这样的示例:
<iron-ajax
auto
url="http://gdata.youtube.com/feeds/api/videos/"
params='{"alt":"json", "q":"chrome"}'
handle-as="json"
on-response="handleResponse"
debounce-duration="300"></iron-ajax>
文档说:
auto
{Boolean
} default:false
If true, automatically performs an Ajax request when either url or params changes.
所以,我认为当你添加“auto
”属性时,它的值默认自动设置为真,即使你绑定了它。这就是为什么我认为你应该删除它。
对不起我的英语,希望你能理解我。
如 auto
属性为真,则 iron-ajax
将在 url[ 时自动执行请求=28=] 或 params 更改。
为了使绑定按您希望的方式工作而无需调用两次,您的绑定应该在 auto
属性的 =
之前有 $
,如下所示:
<iron-ajax
id="ajax"
auto$="{{auto}}"
url="{{url}}"
method="{{method}}"
headers="{{headers}}"
body="{{body}}"
handle-as="json"
content-type="application/json"
on-response="responseHandler"
on-error="errorHandler"
with-credentials>
</iron-ajax>
从Polymer docs开始,像这样绑定的自动属性只有在绑定值为真时才会被设置
聚会有点晚了,但希望这对某人有所帮助