Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_canLoadMore doesn't fetch totalPages #99

Closed
szsoppa opened this issue Oct 28, 2015 · 8 comments
Closed

_canLoadMore doesn't fetch totalPages #99

szsoppa opened this issue Oct 28, 2015 · 8 comments

Comments

@szsoppa
Copy link

szsoppa commented Oct 28, 2015

I have a problem witth totalPages parameter. I use kaminari gem together with grape to serve paginated data from server. Payload header looks like this:

{
  "x-per-page": "20",
  "x-offset": "0",
  "etag": "W/\"e7fd94178cf2d6aa33c6599b64bd29f3\"",
  "vary": "Origin",
  "content-type": "application/json",
  "cache-control": "max-age=0, private, must-revalidate",
  "x-runtime": "0.057154",
  "x-total-pages": "10",
  "x-next-page": "2",
  "x-page": "1",
  "content-length": "6057",
  "x-total": "192",
  "x-request-id": "3d84c4c7-4c8e-4d78-a8c5-ae2c535b09b7"
}

As you can see total-pages is present here but when I stopped _canLoadMore in my route to see what's going on there I got nothing when trying to to this this.get('_totalPages') It seems like it doesn't fetch totalPages. When I return true in _canLoadMore everything works fine.

@kellyselden
Copy link
Collaborator

The response needs to look like this:

{
  items: [
    {id: 1, name: 'Test'},
    {id: 2, name: 'Test 2'}
  ],
  meta: {
    total_pages: 3
  }
}

@szsoppa
Copy link
Author

szsoppa commented Oct 28, 2015

Thank's for reply, works great now :)

@dja
Copy link

dja commented Jun 2, 2016

@szsoppa did you find a good solution for including the pages data in the JSON response under meta? I'm using headers now too, and can't find much on the backend with kaminari or grape on how to structure that properly.

@szsoppa
Copy link
Author

szsoppa commented Jun 2, 2016

I used https://github.com/monterail/grape-kaminari gem to return all of the data required by ember-infinity. You can see in documentation that it's pretty simple to use.

@dja
Copy link

dja commented Jun 2, 2016

Thanks @szsoppa - I'm still not seeing where in grape-kaminari to tell it to include the total_pages in meta as part of the json response.. Is it conflicting with active_model_serializers perhaps?

@szsoppa
Copy link
Author

szsoppa commented Jun 3, 2016

It may have a problem with active_model_serializers. As you can see here https://github.com/monterail/grape-kaminari/blob/master/lib/grape/kaminari.rb#L11 it should include this header out of the box. I used grape-entity gem to serialize data and it worked great.

@dja
Copy link

dja commented Jun 3, 2016

Found this https://github.com/ruby-grape/grape-active_model_serializers#custom-metadata to modify the Rails side, or this http://codeandtechno.com/posts/metadata-via-response-headers-in-emberjs/ to modify the Ember side. For anyone who might need it..

@rmcsharry
Copy link

Be aware that the default is "total_pages" with an underscore and you might be returning dasherized attribute names.

Also, the latest ASM includes links metadata by default, more here on that.

I had this issue and you can read more about it here.

My controller renders like this:

    render json: @employees, meta: { total: (Employee.count / params[:per_page].to_f).ceil }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants