如何从javascript函数内部调用聚合对话框
我总是使用Javascript作用域 我试图在等待JSON响应时显示一个加载对话框,如下所示:如何从javascript函数内部调用聚合对话框,javascript,polymer-2.x,Javascript,Polymer 2.x,我总是使用Javascript作用域 我试图在等待JSON响应时显示一个加载对话框,如下所示: toQueueRequest.onreadystatechange = function () { if (toQueueRequest.readyState === 4 && toQueueRequest.status === 200) { this.$.processingDialog.close();
toQueueRequest.onreadystatechange = function () {
if (toQueueRequest.readyState === 4 && toQueueRequest.status === 200) {
this.$.processingDialog.close();
this.$.confirmDialog.open();
} else if (toQueueRequest.readyState === 4){
this.$.processingDialog.close();
this.$.errorMsg="Ha ocurrido un error!"
this.$.errorDialog.open();
}
};
//var data = JSON.stringify({"RestoCode": window.location.pathname, "Fingerprint": this.finger});
if (this.sector == "Cualquiera") {this.sector = null;};
var data = JSON.stringify({"RestoCode": restoCode, "Fingerprint": finger, "PhoneNumber": this.cellno, "PersonalName": this.name, "QuantityPeople": this.pas, "SectorId": this.sector});
toQueueRequest.send(data);
this.$.reviewDialog.close();
this.$.processingDialog.open();
但是,在onreadystatechange函数中,未定义此.$.ProcessingDialog
我从里面怎么称呼它
非常感谢 这与聚合物本身无关,这是一个关于范围界定的常见问题。这里真正的问题是,在回调
中,this
并不引用您的组件,而是该函数的引用。解决这个问题最常用的方法是在另一个变量中备份上下文。所以在你回电话之前,你会做一些
var that = this;
在你的函数中,你可以使用另一个变量
this.$.processingDialog.close();
变成
that.$.processingDialog.close();
或者,根据您的构建过程或您想要支持的浏览器,如果您可以使用ES6,语法不会创建新的this
,因此您可以更改它
toQueueRequest.onreadystatechange = function () {
// ..
}
到
@伊格纳西奥帕拉维奇尼:不客气,很高兴听到:)
toQueueRequest.onreadystatechange = () => {
// ..
}