Transform JSON to an appropriate format for RESTAdapter EmberJS

后端 未结 4 689
Happy的楠姐
Happy的楠姐 2020-12-18 06:20

I receive a JSON from our API that has the following format

[
  {
    \"id\": 45,
    \"name\": \"Pasta\",
    \"_order\": 0,
    \"is_hidden\": null,
    \         


        
4条回答
  •  醉话见心
    2020-12-18 06:49

    I ran into this issue earlier today. A nice clean way to fix it is to define a normalizePayload method for your ApplicationSerializer. It's made to be overwritten, so you aren't affecting anything else.

    E.g.

    App.ApplicationSerializer = DS.RESTSerializer.extend({
        normalizePayload: function(type, payload) {
            return { category: payload };
        }
    }
    

    If you want to do this on only some of the payloads processed then you just add a conditional inside it.

    App.ApplicationSerializer = DS.RESTSerializer.extend({
        normalizePayload: function(type, payload) {
            if (type.toString() === 'App.Category') {
                return { category: payload };
            }
        }
    }
    

    For more info on the normalizePayload method see http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload

提交回复
热议问题