Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: How to access JSON API metadata from store.findRecord call

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
like image 281
Sarus Avatar asked Nov 12 '15 16:11

Sarus


1 Answers

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.

like image 81
Pavol Avatar answered Oct 25 '22 23:10

Pavol