1

I am working with Ember 1.5.1 and Ember-data 1.0 beta and I am using the DS.RESTADAPTER CLASS. I have two models, let say Post and User. The server replies at a GET request with the following JSON

{
  data: [ .... ]
}

data is an array of users or posts depending on the request.

The RestAdapter is designed around the idea that the JSON exchanged with the server should be conventional, and it expects the JSON returned from your server should look like this

{
  posts: [ .... ]
}

or

{
  users: [ .... ]
}

depending on the request.

How to customize ember-data to handle such situation?

  • http://stackoverflow.com/questions/14300679/how-to-create-a-custom-serializer-for-ember-data – blessanm86 May 18 '14 at 01:00
  • Thanks! Unfortunately at the moment I am not able to test that solution because the server is not ready. As soon as I check it works I'll write the answer. – user3569639 May 19 '14 at 07:36

1 Answers1

1

I was able to handle the situation described on the above question by means of a customization of the extractArray method

// override extractArray method 
App.PostSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, id, requestType) {
    var myposts = payload.data;
    var newpayload = { posts: myposts };
    return this._super(store, type, newpayload, id, requestType);
  }
});

The following resources have been very helpful:

https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray

  • Just for fun, if you're looking to target the `find` method instead of the `findAll` you can use extractSingle. – Nate F. May 06 '15 at 19:41