4
votes

Currently I'm working on app using Rails (4.1.0) API and Ember (2.1.0) and I'm having hard time making pagination in Ember side works.

Rails API JSON output includes automatically made pagination links (from combination of JSON API adapter in AMS and Kaminari pagination) like this:

"links": {
    "self": "http://example.com/articles?page[number]=3&page[size]=1",
    "first": "http://example.com/articles?page[number]=1&page[size]=1",
    "prev": "http://example.com/articles?page[number]=2&page[size]=1",
    "next": "http://example.com/articles?page[number]=4&page[size]=1",
    "last": "http://example.com/articles?page[number]=13&page[size]=1"
  }

Could anyone give me a hint how to get to those values in Ember-Data, in order to use them in Ember template? Has anybody found a temporary workaround for this problem (as Ember is probably going to be fully JSON API compatible one day)?

Every bit of help will be very appreciated ;)

Reference to combination of JSON API adapter in AMS and Kaminari pagination: https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md

Edit:

After making some progress in this matter I think it would be better now to ask:
How to pass hash like page: { number: x, size: y } in params in request to Rails API from Ember?

1
I check this issue: github.com/emberjs/data/issues/2905 . Ember data will have this feature in future. - JeskTop
The accepted answer is spot on. You have to copy links to meta, so you can access that data in the controller/component/template. Here is a guide describing exactly that, with an updated version including a Rails 5 backend with Kaminari: emberigniter.com/pagination-in-ember-with-json-api-backend - Frank Treacy

1 Answers

1
votes

Ember is calling this data that describes the payload: meta/metadata. Hopefully this will help with future searches.

You just have to create a Serializer that intercepts the payload from the server and moves the response.links into Ember Data's metadata.

Ember Guide on metadata: http://guides.emberjs.com/v2.1.0/models/handling-metadata/

Ember Data API's extractMeta method: http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html#method_extractMeta

Alternatively, looking at the JSON API link you posted, you can even change it so the links are replied inside response.meta.

https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md#json-adapter