Active-model-adapter serializer override


#1

OK I’m an Ember NOOB, with capital letters. Just started learning for about a week, so this might be a silly question.

I’m using active-model-adapter with ember-data as well. My backend is with rails 5 api, using active_model_serializers with the json adapter (not json-api). So I have a response like this:

 {
  "products": [
    {
      "id": 8,
      "name": "Yahoo",
      "state": "started",
      "initial_state": "started",
      "state_events": [],
      "events": []
    },
    {
      "id": 9,
      "name": "Linux",
      "state": "kernel",
      "initial_state": "kernel",
      "state_events": [],
      "events": []
    },
    {
      "id": 10,
      "name": "Apple",
      "state": "lion",
      "initial_state": "lion",
      "state_events": [],
      "events": []
    },
    {
      "id": 1,
      "name": "Google",
      "state": "created",
      "initial_state": "initial",
      "state_events": [],
      "events": [
        {
          "id": 4,
          "name": "create",
          "from": "initial",
          "to": "created"
        }
      ]
    }
  ]
}

   {
      "events": [
        {
          "id": 4,
          "name": "create",
          "from": "initial",
          "to": "created",
          "product": {
            "id": 1,
            "name": "Google",
            "state": "created",
            "initial_state": "initial",
            "state_events": []
          }
        }
      ]
    }

Can someone help me override the serializer so I can get this to work with this models:

app/models/products.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  state: DS.attr('string'),
  initialState: DS.attr('string'),
  state_events: DS.attr(),
  events: DS.hasMany('event')
});

app/models/events.js
import DS from 'ember-data';
export default DS.Model.extend({
  name: DS.attr('string'),
  from: DS.attr('string'),
  to: DS.attr('string'),
  product: DS.belongsTo('product')
})