Skip to content

Instantly share code, notes, and snippets.

@simonexmachina
Created March 4, 2014 22:59
Show Gist options
  • Save simonexmachina/9357528 to your computer and use it in GitHub Desktop.
Save simonexmachina/9357528 to your computer and use it in GitHub Desktop.
Ember Data Pagination

Ember Data's moved on quite a bit since I wrote my pull request, so I'd need to do it again. I've decided to lay out the case here and if you and the other members of the team feel it's a good idea then I'll do it again.

Here's the approach that I'm advocating:

  • Whenever the store asks the adapter to fetch some records a Request object (a new class) is created that contains the information required to request another page of results.
  • The Request object is also used to contain the sinceToken that's currently there
  • The meta object that's returned from the server is then attached to the Request object, which is attached to the returned RecordArray so that downstream users of the RecordArray (eg. ArrayControllers) have access to the page number, page size and the total number of records
  • RecordArrays would have two new methods: fetchPage() and fetchMore(), which would use the information in the Request object to perform another fetch from the server.
  • Adapters can control the parameter names that are used for pagination, and can also specify a default page size. By default adapters would not ask for any paging from the server unless requested in the call to store.find...().

I've implemented this on the findQuery() method, and it wouldn't be hard to extend the approach to the other find...() methods (findMany(), findHasMany() et al). I've used this approach to implement normal paging, and an "infinite scroll" approach, and it's very simple and works really well.

I think this is the right solution, it's very minimal and allows for innovation to happen in userland. There's interest in this feature from the community. I think it'd be a real shame if Ember Data was released without basic support for pagination.

If we can get agreement on the right approach then I'll find the time to write the PR.

@koemeet
Copy link

koemeet commented Oct 17, 2014

Sounds great. 👍 But how about linked relations? Say you have an application record which has an async relation with pages. If you would do this application.get('pages') it would make a call to /applications/1/pages. What if you need to paginate that?

I've implemented a new method on the Model, called getPaginated. You could say application.getPaginated('pages', { page: 2 }).

Did you made any progress with pagination in ED? It's been a time since you started this :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment