[英]Ember return model with 2 promises
I have a route class and in the template, I use a custom grid component 我有一个路由类,在模板中,我使用了一个自定义的网格组件
{{my-grid params=this.gridParams elementId='myGrid'}}
Now, there are 2 AJAX calls to be made to populate the grid; 现在,要进行2次AJAX调用以填充网格。
1. /getColumns (this column header data response is used to set some properties on my controller)
2. /getData (this body response is actually used to populate the grid with actual data and is actually computed to gridParams)
I was reading the guide on "The Router Pauses for Promises" https://guides.emberjs.com/v2.2.0/routing/asynchronous-routing/ 我正在阅读有关“路由器暂停承诺”的指南https://guides.emberjs.com/v2.2.0/routing/asynchronous-routing/
However, this is for a single promise/ajax call. 但是,这仅用于一个promise / ajax调用。
How can I make it work in my case ? 我该如何使其适用于我的情况?
UPDATE UPDATE
My common single POST request 我常见的单个POST请求
doPostReq: function(postData, requestUrl){
var promise = new Ember.RSVP.Promise(function(resolve, reject) {
return $.ajax({
}).success(resolve).error(reject);
})
return promise;
},
if you have two or more promises and want to know when they all resolved use RSVP.hash or RSVP.all 如果您有两个或多个诺言,并且想知道它们何时全部解决,请使用RSVP.hash或RSVP.all
model() {
return Ember.RSVP.hash({
columns: this.getColumns(),
data: this.getData()
});
}
in your controller now you can use resolved promises like model.columns
and model.data
现在,您可以在控制器中使用
model.columns
和model.data
等model.columns
解决的model.data
UPDATE UPDATE
if you want serial execution (one promise after another) then you can do : 如果要串行执行(一个答应一个接一个),则可以执行以下操作:
model() {
let data;
return this.getData().then(function(d){
data = d;
return this.getColumns();
}).then(function(columns){
return { columns, data };
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.