0

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;

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/

However, this is for a single promise/ajax call.

How can I make it work in my case ?

UPDATE

My common single POST request

doPostReq: function(postData, requestUrl){
var promise = new Ember.RSVP.Promise(function(resolve, reject) {
    return $.ajax({            
    }).success(resolve).error(reject);
})

return promise;
},
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • Possible duplicate of [EmberJS: How to load multiple models on the same route?](http://stackoverflow.com/questions/20521967/emberjs-how-to-load-multiple-models-on-the-same-route) – Patsy Issa Jan 19 '16 at 16:26

1 Answers1

3

if you have two or more promises and want to know when they all resolved use RSVP.hash or 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

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 };
 });
}
Bek
  • 3,157
  • 1
  • 17
  • 28
  • Cool..thx..Just 2 more questions ; 1. I want data to be called after columns. so how can I handle that ? AND 2. Can I convert this hash solution to some form of common code like I have for a single request currently; doPostReq (pls see my UPDATE) – copenndthagen Jan 19 '16 at 12:24
  • if you want serial execution on after another then you don't need any helper functions from RSVP actually – Bek Jan 19 '16 at 14:11
  • i mean it is an AJAX server call for both the requests...which is the reason i wanted to use Ember.RSVP.Promise and use $.ajax() inside of it – copenndthagen Jan 19 '16 at 14:29