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

Option to use meta data from headers #197

Closed
DylanGuedes opened this issue Dec 7, 2016 · 3 comments
Closed

Option to use meta data from headers #197

DylanGuedes opened this issue Dec 7, 2016 · 3 comments

Comments

@DylanGuedes
Copy link

Hi,

I'm trying to consume an API that maintains meta-data like 'totalPages', 'currentPage', 'totalEntries' via the response header, instead of the response body. Any possibility to also work on headers? :/

@hhff
Copy link
Collaborator

hhff commented Dec 11, 2016

Hi @DylanGuedes ! I'm not too sure how you'd do this, but it's outside of the scope of Ember Infinity - Ember Data doesn't let headers get back to the App code.

I'd instead take a look at the serializer for your model, and perhaps "transform" the headers from the response into an entry on the model collection's meta.

@hhff hhff closed this as completed Dec 11, 2016
@edprats
Copy link

edprats commented Jan 7, 2017

i had a similar issue and here was my solution for future googlers.

place this in the corresponding adapter and you are good to go.

// adapters/foo.js
// this is for a RESTAdapter

handleResponse: function(status, headers, payload) {
    if (headers['X-Total']) {

      let meta = {
        'total': parseInt(headers['X-Total']),
        'total_pages': parseInt(headers['X-Total-Pages']),
        'per_page': parseInt(headers['X-Per-Page']),
        'starting_page': headers['X-Starting-Page'],
        'page': parseInt(headers['X-Page'])
      };

      payload.meta = meta;
    }

    return this._super(status, headers, payload);
  }

now a word to the wise as a follow up.

these 'custom headers' you will be using for ember infinity will not appear as visible 'headers' by default. you will need to have your backend response header allow for it in order for ember to see them.

in the case of the snippet posted above the response header also should also include
Access-Control-Expose-Headers:X-Total, X-Per-Page, X-Page, X-Total-Pages, X-Starting-Page

see this for details:
http://stackoverflow.com/questions/37505086/how-to-view-all-response-headers-in-ember-with-restadapter

@hhff
Copy link
Collaborator

hhff commented Jan 7, 2017

+1 to @edprats solution - I've actually used that in the past :)

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

3 participants