0
votes

I am a bit confused and just need some clarity: Should I be implementing a custom adapter or a custom serializer?

I need my ember app to talk to a REST/json backend.

I want my ember app to expose the resource as:

GET /locations/:id

But the host it connects to has the resource located at:

GET http://server/api/location/:id

Payload from the server:

{
  "id": "7ff3269e-d16c-4cc4-954d-aef8e662e0f6",
  "geo": {
    "latitude": 0,
    "longitude": 0
  },
  "typedAddress": {
    "addressType": "US",
    "countryCode": "US",
    "name": "string",
    "address1": "string",
    "address2": "string",
    "address3": "string",
    "postalCode": "string"
  },
  "customDescription": "string",
  "timezone": "string"
}

My model in ember for this:

export default Model.extend({
  latitude: attr('number'),
  longitude: attr('number'),
  addressType: attr('string'),
  countryCode: attr('string'),
  address1: attr('string'),
  address2: attr('string'),
  address2: attr('string'),
  city: attr('string'),
  state: attr('string'),
  briefPostalCode: attr('string'),
  postalCode: attr('string'),
  timezone: attr('string')
});
1

1 Answers

4
votes

You just need to use the RESTAdapter and write your own Serializer.

Suppose your model type is "location". You would have a app/serializers/location.js like this:

export default DS.RESTSerializer.extend({
    normalizeResponse(store, primaryModelClass, payload, id, requestType) {
        let data= {
            locations: [{
                id: payload.id,
                latitude: payload.geo.latitude,
                // etc. for all your properties
            }]
        };
        return this._super(store, primaryModelClass, data, id, requestType);
    }
});