lundi 7 janvier 2019

ember data different models for same endpoint

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.




Aucun commentaire:

Enregistrer un commentaire