在 polymer 2.0 中同步调用 Rest API
Synchronous call to Rest API in polymer 2.0
谁能告诉我如何在 polymer 2.0 中对 Rest 服务进行同步调用。
我正在尝试计算客户总余额,我只能在收到以下所有三个 Rest 服务的响应后才能执行此操作。
有什么方法可以使用一些 api 来同步它们吗?承诺或任何其他
<iron-ajax id="balanceAjax" url="/balances" last-response="{{res1}}"></iron-ajax>
<iron-ajax id="currencyAjax" url="/currencies" last-response="{{res2}}"></iron-ajax>
<iron-ajax id="rateAjax" url="/rates" last-response="{{res3}}"></iron-ajax>
您可以对所有三个属性(res1、2 和 3)使用相同的观察者函数,并且在观察者中,如果它们都有一些值,则继续计算该值。
res1: {
observer: 'responseChanged'
},
res2: {
observer: 'responseChanged'
}
res3: {
observer: 'responseChanged'
},
...
responseChanged: function() {
if(this.res1 && this.res2 && this.res3) {
//calculate total balance
}
}
你也可以有一个观察者而不是三个
res1: {
},
res2: {
}
res3: {
},
...
observers: [responseChanged(res1, res2, res3)],
responseChanged: function() {
if(this.res1 && this.res2 && this.res3) {
//calculate total balance
}
}
或者,您也可以使用 iron-ajax
的 on-response
侦听器。
最后,如果你的 res1 , 2 和 3 甚至在 api 调用之前就具有价值,你可以在每个 ajax 的 on-response
监听器中设置一些布尔值,并在这些布尔值上设置观察者。
谁能告诉我如何在 polymer 2.0 中对 Rest 服务进行同步调用。
我正在尝试计算客户总余额,我只能在收到以下所有三个 Rest 服务的响应后才能执行此操作。
有什么方法可以使用一些 api 来同步它们吗?承诺或任何其他
<iron-ajax id="balanceAjax" url="/balances" last-response="{{res1}}"></iron-ajax>
<iron-ajax id="currencyAjax" url="/currencies" last-response="{{res2}}"></iron-ajax>
<iron-ajax id="rateAjax" url="/rates" last-response="{{res3}}"></iron-ajax>
您可以对所有三个属性(res1、2 和 3)使用相同的观察者函数,并且在观察者中,如果它们都有一些值,则继续计算该值。
res1: {
observer: 'responseChanged'
},
res2: {
observer: 'responseChanged'
}
res3: {
observer: 'responseChanged'
},
...
responseChanged: function() {
if(this.res1 && this.res2 && this.res3) {
//calculate total balance
}
}
你也可以有一个观察者而不是三个
res1: {
},
res2: {
}
res3: {
},
...
observers: [responseChanged(res1, res2, res3)],
responseChanged: function() {
if(this.res1 && this.res2 && this.res3) {
//calculate total balance
}
}
或者,您也可以使用 iron-ajax
的 on-response
侦听器。
最后,如果你的 res1 , 2 和 3 甚至在 api 调用之前就具有价值,你可以在每个 ajax 的 on-response
监听器中设置一些布尔值,并在这些布尔值上设置观察者。