Manual Pagination in Ember Data Relationships

Hey folks :wave:

I asked a question in Discord a few months back about this but it didn’t get any response :tired_face: To be honest it is completely fair because it is a bit of a sticky issue and this is a better medium to have a slightly longer conversation about it.

I’m trying to figure out a good way to properly paginate through a relationship in an ember-data model. I have a slight preference for doing this manually (because I’m actually working with a statically generated backend here), but at this stage I would just like to get anything to work :see_no_evil:

The first thing that I have tried is that I have made sure that I make use of the reference API and I can get the first 10 ids of the objects in my relationship and load them one by one. Here is the code that I’m using to load the data:

let ids = this.args.tag.hasMany('posts').ids().slice(0, 10);

let promise = Promise.all(ids.map(id => this.store.findRecord('content', id)));

this.topPosts = DS.PromiseArray.create({
  promise,
});

(yes it’s a blog system and I know it’s a cliché for examples like this but I’m actually trying to do this with a blog :joy:)

The problem is that over the course of the app lifecycle it seems like the tag.hasMany('posts') array is getting out of order compared to what is coming back from the server :thinking: I have mentioned this to a few people in the Ember Community and they have all said something along the lines of “you can’t rely on the order in the JSON:API response” :upside_down_face:

My question is this: is there any way that I can access this ordered list from the JSON:API response again or is it shredded somewhere in the ember-data internals?

Any help would be greatly appreciated :tada:

1 Like