jeudi 13 juillet 2017

Handle multiple API in a single app with similar model type

I have two APIs, let's say :

  • http://ift.tt/2tRjIT7
  • bbb.com/api/events

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

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

  • models/aaa/event.js
  • models/bbb/event.js

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 http://ift.tt/2tRjIT7, the returned payload is typed events but Ember fails to translate it to aaa/events.

How can I proceed ?




Aucun commentaire:

Enregistrer un commentaire