Writing adapter for my API


#1

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!


#2

First of all, since your backend is on a different domain make sure you enable CORS - http://www.yiiframework.com/doc-2.0/yii-filters-cors.html

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'
});

#3

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


#4

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

shouldReloadAll()
 { 
      return true; 
 }

#5

an example for reference

import DS from 'ember-data';

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