how to really find out only once if Ember finishes its ajax request and DS.RecordArray is completely filled

201 Views Asked by At

I have probably tried a gazillion ways and nothing is efficient. My last attempt - which works a little well but with an ugly tradeoff was this:

App.UsersRoute = Em.Route.extend({
  model: function() {
    return App.User.find({}).then(function(response) {
      return response;
    });
  }
});

the problem with this - which I'd love to know is its making a synchronous call. My HTML/DOM won't finish loading until this returns.

Another thing I'd love to know is if I omit the empty object {} from the find - the promise function actually gets called immediately. I promise!

Now other methods I've tried are the following which all have flaws:

  • observing content.lastObject.isLoaded on a controller
  • implementing arrayContentDidChange from Ember.ArrayController - obviously this gets triggerd multiple times as the array is getting filled. -
2

There are 2 best solutions below

1
On

You can read about it here https://github.com/emberjs/data/pull/735 There is a question like yours Ember-data RecordArray isLoaded Status. Also you can try to observe isUpdating to be false instead of isLoading

8
On

I have probably tried a gazillion ways and nothing is efficient.

I don't know if you tried hooking into the afterModel function of a route which is an addition added not very long ago and available in rc6:

App.UsersRoute = Em.Route.extend({
  afterModel: function(users, transition) {
    console.log(users.get('length'));
  }
});

See here for more info on the hooks beforeModel and afterModel.

I've also put togheter a jsbin to play around.

Hope it helps.