1

I am trying to hook up Ember Data to work with an existing REST API. The problem that I am running into is that the REST implementation does not conform to how Ember Data expects things to be done. I have scoured the web for documentation that would suggest how to make things work, but short of writing my own DS.Adapter implementation I am at a loss.

Here is what my request looks like:

/api/user/12345

which provides the following response:

{
    data: {
        ID: '12345',
        firstName: 'Fred',
        lastName: 'Flintstone',
        emailAddr: '[email protected]'
    }
}

Ember expects "data" to be "user". Unfortunately I cannot easily change the API. Any suggestions?

Thanks

1 Answer 1

2

One way I can think of you could achieve this, would be by creating your own serializer and override the extract function:

App.RESTSerializer = DS.RESTSerializer.extend({
  extract: function(loader, json, type, record) {
    var root = 'data';

    if (json[root]) {
      if (record) { loader.updateId(record, json[root]); }
      this.extractRecordRepresentation(loader, type, json[root]);
    }
  }
});

App.Store = DS.Store.extend({
  adapter: DS.RESTAdapter.extend({
    serializer: App.RESTSerializer.create()
  })
});

Please note that this modification assumes that the content of your request will always be under the data key of your JSON.

Also worth mentioning is that the original extract method has two lines which are not included in the example:

 this.sideload(loader, type, json, root);
 this.extractMeta(loader, type, json);

this makes you loose side loading functionality and metadata extraction. I hope loosing this functionality is not a show stopper for your use case.

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

@mzabriskie, glad I could help, plase don't forget to mark the answer as accepted, so future folks stumbling upon this will know it worked for you, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.