Writing adapter for my API

I have an API that does not follow JSONAPI format. Its written in Yii2 and so it have a consistent format. Here is an example:

api/v1/companies

[
  {
    "id": 1,
    "name": "Company 1",
    "contact": "P.O. Box 1234 MZA", 
    "create_time": 1234,
    "update_time": 1234
  },
  {
    "id": 2,
    "name": "Company 2",
    "contact": "P.O. Box 5678 DSM", 
    "create_time": 1234,
    "update_time": 1234
  }
]

and getting single model

api/vi1/companies/1

{
    "id": 2,
    "name": "Company 2",
    "contact": "P.O. Box 5678 DSM", 
    "create_time": 1234,
    "update_time": 1234
  }

I need now to configure the right adapter to help me handle my requests (that is just single endpoint only). Looking at documentation I have three choices. JSONAPI seems to be a wrong choice in my case and am confused with the two as to which one I should use and how to do it.

My Ember app sits in different domain (hence URL) from the API domain. I would appreciate also any link that explains something near to my use case.

Its my first time to try out Ember-data so if it sound as a fool’s question, please bear with me! Thanks a lot!

First of all, since your backend is on a different domain make sure you enable CORS - Cors, yii\filters\Cors | API Documentation for Yii 2.0 | Yii PHP Framework

For your case I would recommend using the RESTAdapter using the JSONSerializer:

// app/adapters/application.js
import DS from 'ember-data';

export default DS.RESTAdapter.extend({
    defaultSerializer: 'JSONSerializer',
    host: 'mycustomdomain.com',
    namespace: 'api/v1'
});
1 Like

Only that simple? Wow! I’ll try it out

Its working just fine. I found though deprecation notice and I added:

shouldReloadAll()
 { 
      return true; 
 }

an example for reference

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
    defaultSerializer: 'JSONSerializer',
    host: 'http://localhost:8081/index.php',
    namespace: 'v1'
});