Ember data serialzier


#1

I have a pre-built JSON api. Here is an example of the payload:

item:

{
    "data": {
        "id": 1,
        "username": "rodzzlessa",
        "slug": "rodzzlessa"
     }
}

collection:

{
"data": [
    {
        "id": 34,
        "name": "HDG common nails 50lbs",
        "slug": "hdg-common-nails-50lbs4569",
        "description": "Accusantium ipsam impedit omnis sint dolorum.",
        "image_src": "nail-box-angle.png",
        "categories": {
            "data": [
                {
                     "id": 2,
                     "name": "nails",
                    "image_src": "nails-icon.png"
                }
             ]
         }
     }
  ]
}

I know I need to create a custom ember data serializer. I’m just having trouble understanding actually how to do it. I looked online but there are no good resources/tutorials. Is there an ember data book? or online resource to help me with my issue? From what I understand I need to edit the extractArray and extractSingle methods. I saw the ember-data API guide but it was just confusing I have no idea what to do. If anyone could point me to any good resource on the topic I would really appreciate it. Thank you


#2

I have been able to create a per model basis serializer which works for now. The only problem I’m having is dealing with relationships:

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
	extractArray: function(store, typeClass, payload) {
	    payload.products = payload.data;
	    delete payload.data;

	    return this._super(store, typeClass, payload);
	}
});

I have a categories: DS.hasMany('category') now I’m trying to figure out which method to edit to let the payload know that they need to be grabbing categories.data instead of just categories since its not an array.


#3

Sorry, this is probably slightly off topic and might not cover your case, but the following slides could be beneficial: http://www.slideshare.net/JeremyGillick/normalizing-data. The basic idea is to side-load the relationships.