Christian Fazzini
Christian Fazzini

Reputation: 19723

How is a json paginated resource supposed to look like?

With Ember Data and Jsonapi. How is a json paginated resource supposed to look like?

I built my response so it looks like:

"meta": {
  "page": {
    "number": 1,
    "size": 5,
    "total": 39
  }
},
"links": {
  "self": "http://localhost:3099/api/v1/articles",
  "prev": null,
  "next": "http://localhost:3099/api/v1/articles?page[number]=2",
  "first": "http://localhost:3099/api/v1/articles?page[number]=1",
  "last": "http://localhost:3099/api/v1/articles?page[number]=39"
},
"data": [
  ...
]

But I am not exactly sure if this is the right format. based on the explanation at http://jsonapi.org/format/#fetching-pagination

Or, are the pagination links (i.e. prev, next, first and last) supposed to be in meta.page ?

Upvotes: 2

Views: 112

Answers (2)

SeanK
SeanK

Reputation: 667

Ember Data doesn't follow the JSON spec strictly so you should concentrate more on setting up the JSON with what ED needs. I would personally move the 'links' info into the meta tag. Otherwise Ember-Data will attempt to put them into a model called 'links', which may not be what you want. If you do intend to store those inside a separate 'links' model, then what you have is fine.

Upvotes: 1

Jonathan Chan
Jonathan Chan

Reputation: 354

You could use ember-cli-pagination and its format to do pagination. I'm pretty sure Ember Data does not follow the JSON API spec strictly.

Based on your sample this could be a format:

{
  "meta": {
    "total_pages": 3,
    "page": 1
  },

  "articles": [
    {"id": 1, "title": "Hello World", "body": "More to Come"},
    // ......
  ]
}

The request URL of this payload could be http://localhost:3099/api/v1/articles?page=1. See the API for more info.

Upvotes: 1

Related Questions