Polymer 1.0:多次调用 iron-request 的 send() 方法
Polymer 1.0: Multiple calls to send() method of iron-request
我有一个组件使用 <iron-ajax>
的实例从后端检索数据,我想使用 <iron-request>
发送更新,例如 POST/DELETE 请求.
第一次一切都完美无缺。但是,如果再次调用请求,我会收到错误消息:
Uncaught TypeError: Cannot read property 'then' of undefined
我的模板定义如下所示:
...
<iron-ajax id="ajax" auto verbose
url="/cart-api/"
last-response="{{ajaxResponse}}"
handle-as="json">
</iron-ajax>
<iron-request id="xhr"></iron-request>
...
在我的组件脚本中,我使用 <iron-request>
的 send()
方法发送 POST:
var me = this;
this.$.xhr.send({
url: "/cart-api",
method: "POST",
body: JSON.stringify(entry)
}).then(function() {
me._refresh();
}, function() {
console.error("POST failed");
});
错误消息表明 send
返回了 undefined
而不是有效的 Promise 对象。
所以我的问题是:<iron-request>
元素真的可以重用吗?我需要做些什么来刷新或重新初始化它吗?
更新
感谢@Zikes,我更新了我的代码如下:
<iron-ajax id="ajaxGet" auto
url="/cart-api/"
last-response="{{ajaxResponse}}"
handle-as="json">
</iron-ajax>
<iron-ajax id="ajaxPost" url="/cart-api" method="POST" on-response="_refresh"></iron-ajax>
<iron-ajax id="ajaxDelete" method="DELETE" on-response="_refresh"></iron-ajax>
insertEntry: function(entry) {
this.$.ajaxPost.body = JSON.stringify(entry);
this.$.ajaxPost.generateRequest();
},
_handleRemove: function(e) {
var entry = e.currentTarget.entry;
this.$.ajaxDelete.url = "/cart-api/" + entry.id;
this.$.ajaxDelete.generateRequest();
},
_refresh: function() {
this.$.ajaxGet.generateRequest();
},
<iron-ajax>
组件为每个新请求生成一个新的 <iron-request>
:https://github.com/PolymerElements/iron-ajax/blob/master/iron-ajax.html#L442
如果您正在寻找可重用性(以及去抖动等其他不错的功能),您最好使用 <iron-ajax>
组件。
我有一个组件使用 <iron-ajax>
的实例从后端检索数据,我想使用 <iron-request>
发送更新,例如 POST/DELETE 请求.
第一次一切都完美无缺。但是,如果再次调用请求,我会收到错误消息:
Uncaught TypeError: Cannot read property 'then' of undefined
我的模板定义如下所示:
...
<iron-ajax id="ajax" auto verbose
url="/cart-api/"
last-response="{{ajaxResponse}}"
handle-as="json">
</iron-ajax>
<iron-request id="xhr"></iron-request>
...
在我的组件脚本中,我使用 <iron-request>
的 send()
方法发送 POST:
var me = this;
this.$.xhr.send({
url: "/cart-api",
method: "POST",
body: JSON.stringify(entry)
}).then(function() {
me._refresh();
}, function() {
console.error("POST failed");
});
错误消息表明 send
返回了 undefined
而不是有效的 Promise 对象。
所以我的问题是:<iron-request>
元素真的可以重用吗?我需要做些什么来刷新或重新初始化它吗?
更新
感谢@Zikes,我更新了我的代码如下:
<iron-ajax id="ajaxGet" auto
url="/cart-api/"
last-response="{{ajaxResponse}}"
handle-as="json">
</iron-ajax>
<iron-ajax id="ajaxPost" url="/cart-api" method="POST" on-response="_refresh"></iron-ajax>
<iron-ajax id="ajaxDelete" method="DELETE" on-response="_refresh"></iron-ajax>
insertEntry: function(entry) {
this.$.ajaxPost.body = JSON.stringify(entry);
this.$.ajaxPost.generateRequest();
},
_handleRemove: function(e) {
var entry = e.currentTarget.entry;
this.$.ajaxDelete.url = "/cart-api/" + entry.id;
this.$.ajaxDelete.generateRequest();
},
_refresh: function() {
this.$.ajaxGet.generateRequest();
},
<iron-ajax>
组件为每个新请求生成一个新的 <iron-request>
:https://github.com/PolymerElements/iron-ajax/blob/master/iron-ajax.html#L442
如果您正在寻找可重用性(以及去抖动等其他不错的功能),您最好使用 <iron-ajax>
组件。