Reputation: 5533
I have an API that doesn't return JSON data in a format that Ember-Data expects. Especially when getting a list of resources versus a single resource.
For example, GET /api/widgets/{id}
Should return a single widget model that might look like this:
//app/models/widget.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
weight: DS.attr('number'),
color: DS.attr('string')
});
Whereas getting the full list of widgets via GET /api/widgets/
returns a model that should look like this:
// app/models/widgetlist.js
import DS from 'ember-data';
export default DS.Model.extend({
total: 22,
widgets: DS.hasMany('widget')
});
(I think that's how my widget list model should look. It's essentially a total count of widgets in system and current paginated set of widgets)
I'm having a really hard time figuring out what combination of models, custom adapter and/or custom serializer I need in order to get this to work.
EDIT:
// Server responses examples
// GET api/widgets/77
{
"id":77,
"name":"Acoustic Twangdoodle",
"weight":3,
"color":"purple"
}
// GET api/widgets/
{
"total":22,
"widgets":[
{
"id":77,
"name":"Acoustic Twangdoodle",
"weight":3,
"color":"purple"
},
{
"id":88,
"name":"Electric Twangdoodle",
"weight":12,
"color":"salmon"
}
]
}
Upvotes: 1
Views: 172
Reputation: 18240
Thats only one model!
Now I don't see how your pagination works. Depending on that maybe you should not use findAll
but instead use query
to load a paginated set.
The total
is not part of the model but of the metadata. Use a custom JSONSerializer
and let extractMeta
return this.
Depending how your pagination works you wanna do something like store.query('widget', { page: 3 })
. If you speak more about how to access page 2 or so it will be easier to explain this.
Upvotes: 2