2

Ember controllers are promise-aware, in that the model can be a promise. For an ArrayController you could have a promise to an array, but what if you have an array containing promises?

See this JSFiddle for an example http://jsfiddle.net/7QSZ6/

Why do I need this? We have a Hypermedia (HAL) API with pagination, so if I request a collection of 500 items I get back the first 50 items, with a link to the next 50, and the total count.

I would like to create an array-like object based on Ember.Array that lazily loads consecutive pages. But since loading is async objectAt would have to return a promise that resolves when the element arrives.

Roughly in pseudocode

MyArray = Ember.Object.extend(Ember.Array({
  length: 500,
  retrieved: 50,

  objectAt: function(i) {
    if (i > this.get('retrieved')) {
      retrievePageFor(i);
    }
    return this.get('promiseArray')[i];
  },

  retrievePagesFor: function(i) {
    // request pages until we have a value for index i
    // update 'retrieved'
    // populate 'promiseArray' with promises that resolve when the data arrives
  }
}));

Update:

One way is to do it "manually" by using PromiseProxyMixin, e.g. http://jsfiddle.net/Au7LC/1/

Arne Brasseur
  • 1,468
  • 13
  • 18
  • This would imply a request for each item, not page. Your case doesn't sound that unusual at all. I would think you can just use a standard ArrayController where you load the first page, and then fire off additional page loads, appending objects to the `controller.content` as they resolve. – aceofspades Jun 30 '14 at 18:30
  • That would be eager loading everything. The UI has its own independent pagination, possibly with different page size. I need to be able to render page buttons (hence know total size upfront), but only load as much content as is needed to render a specific page. – Arne Brasseur Jul 01 '14 at 06:50
  • 1
    Trigger loads when you want, but push results into the ArrayController's content when they resolve. Push placeholder objects in as you issue reloads, if that's what you're thinking, and then replace them as the promises resolve. Good luck. – aceofspades Jul 01 '14 at 14:32

0 Answers0