Valentin Trinqué
Valentin Trinqué

Reputation: 1072

Handle multiple API in a single app with similar model type

I have two APIs, let's say :

Both implement JSONAPI adapter, and then return a payload with type="events".

As a result, I have to scope those models into subfolders:

Which result in querying the models this way:

this.get('store').findAll('aaa/event');

Then, I have implemented an adapter:

// adapters/aaa/event.js

import config from '../../config/environment';
import DS from 'ember-data';
import Ember from "ember";

const { String } = Ember;

export default DS.JSONAPIAdapter.extend({
  host: 'aaa.com',
  namespace: 'api/v1',

  pathForType: function(type) {
    return String
      .underscore(type) // `aaa/some-model` -> `aaa/some_model`
      .pluralize() // `aaa/some_model` -> `aaa/some_models`
      .replace("aaa/", ""); // `aaa/some_model` -> `some_model`
  },
});

So far so good, Ember correctly calls aaa.com/api/v1/events, the returned payload is typed events but Ember fails to translate it to aaa/events.

How can I proceed ?

Upvotes: 2

Views: 243

Answers (1)

Ember Freak
Ember Freak

Reputation: 12872

So you got the model specific serializer for aaa/event and bbb/event. you can overridre modelNameFromPayloadType method to translate events to your required model name aaa/events.

For aaa/event serializer,

modelNameFromPayloadType(payloadType) {
  return 'aaa/event';
}

Refer:
https://emberjs.com/api/ember-data/2.14/classes/DS.JSONAPISerializer/methods/modelNameFromPayloadType?anchor=modelNameFromPayloadType

Upvotes: 1

Related Questions