I am using the Ember-Data Rest-Adapter and the JSON returned from my server looks basically like the one in the Active Model Serializers Documentation
{ "meta": { "total": 10 }, "posts": [ { "title": "Post 1", "body": "Hello!" }, { "title": "Post 2", "body": "Goodbye!" } ] }
Fetching the data from the server works but unfortunately I am not able to figure out where I can access the meta information from my JSON response.
Based on my research in ember-data's github issue, support for meta information seems to be implemented with commit 1787bff.
But even with the test cases I was not able to figure out how to access the meta information.
App.PostController = Ember.ArrayController.extend({ .... requestSearchData: function(searchParams){ posts = App.Post.find(searchParams); this.set('content', posts); // don't know how to access meta["total"] // but I want to do something like this: // this.set('totalCount', meta["total"]) } })
Can anybody of you shed some light on this for me, please? I am aware that the Ember api is moving fast but I am sure I am just missing a small part and that this is actually possible.
A metadata file written in JSON is used to configure the fields and categories for document abstraction. The file configures the field categories and lists available, as well as what type of values are permitted for each field.
If metadata is refers to the whole response you could add it as header fields. If metadata refers only to part of the response, you will have to embed the metadata as part of the object. DON'T wrap the whole response in an artifical envelope and split the wrapper in data and metadata.
I found a cleaner approach for extracting meta information from the server response with ember-data.
We have to tell the serializer which meta-information to expect (in this case pagination):
App.serializer = DS.RESTSerializer.create(); App.serializer.configure({ pagination: 'pagination' }); App.CustomAdapter = DS.RESTAdapter.extend({ serializer: App.serializer }); App.Store = DS.Store.extend({ adapter: 'App.CustomAdapter' });
After that every time the server sends a meta-property with a pagination object this object will be added to the store's TypeMaps
property for the requested Model-Class.
For example with the following response:
{ 'meta': {'pagination': { 'page': 1, 'total': 10 } }, 'posts':[ ... ] }
The TypeMap
for the App.Post-Model would include the pagination object after the posts have loaded.
You can't observe the TypeMaps-property of the store directly so I added an computed property to the PostsController to have access to the requests pagination meta information:
App.PostsController = Ember.ArrayController.extend({ pagination: function () { if (this.get('model.isLoaded')) { modelType = this.get('model.type'); this.get('store').typeMapFor(modelType).metadata.pagination } }.property('model.isLoaded') });
I really don't think that's a great solution to the meta information problem but this is the best solution I was able to come up with yet with Ember-Data. Maybe this will be easier in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With