6

I am currently trying to figure out how to access metadata when using the store.findRecord() call.

In the guides (http://guides.emberjs.com/v2.1.0/models/handling-metadata/) it says that metadata can be accessed by doing the following:

store.query('post').then((result) => {
  let meta = result.get('meta');
})

I was hoping this would work when using findRecord as well

store.findRecord('book', params.id, {adapterOptions: {query: params}}).then((result) => {
    let meta = result.get('meta');
})

However this always returns undefined for the meta property. I'm assuming that metadata is not being set when using findRecord. I am returning valid JSON-API with a meta property at the top level like this:

{
  "meta": {
    "page": {
      "number": 1,
      "size": 100,
      "total": 20
    },
   "data":[
       // data here
    ]
  }
}

Is there a way to access the metadata returned by the server when using findRecord() and the JSONAPISerializer and JSONAPIAdapters?

Thank you!

I am using the following versions:

Ember             : 2.1.0
Ember Data        : 2.1.0
jQuery            : 1.11.3
Sarus
  • 3,093
  • 1
  • 21
  • 25
  • Is the record already present in the store when you're doing the findRecord? – Tyler Iguchi Nov 12 '15 at 20:57
  • Good thought but it's not already present in the store. I also checked to make sure in the chrome inspector that the network request is being made and it is. I'm starting to think this is just not supported right now. Based on some discussions in slack it seems like the meta key only works on collections. – Sarus Nov 12 '15 at 20:59
  • Take a look at the Ember Guides on `query` vs `queryRecord` vs `findRecord` etc. I think what you're trying to do with `findRecord` isn't possible: http://guides.emberjs.com/v2.1.0/models/finding-records/ – vikram7 Nov 12 '15 at 21:47
  • @vikram7 Yes, I think findRecord doesn't support metadata out of the box. I'm curious as to whether or not there is a workaround. – Sarus Nov 12 '15 at 23:27
  • You could override the serializer – andorov Nov 14 '15 at 00:04
  • I may be late here, but... I don't know ember data well, but it seems like, even if it did expose the metadata with findRecord, it would expose the metadata from inside the resource object (i.e. within `"data"`) rather than from the top-level, right? – Ethan Nov 29 '15 at 08:50

1 Answers1

2

metadata are not supported per-record basis, you can count of having them available if you use store.query() or when fetching relationships. Quoting:

For now, Ember Data only understands a top level meta object on queries and relationships (through related links).

Take a look at the following github thread for more information.

Pavol
  • 1,170
  • 8
  • 18
  • 2
    FYI I posted here on a way you can accomplish this: http://stackoverflow.com/questions/35514055/how-to-access-top-level-meta-key-from-json-api-servers-200-response-when-callin/38312846#38312846 – Sarus Jul 11 '16 at 17:34