Delete JSON root element for POST/PUT operations in Ember Data

旧城冷巷雨未停 提交于 2019-12-21 03:28:42

问题


I'm consuming a web service that in POST/PUT verbs expects a JSON like this:

{
    "id":"CACTU",
    "companyName": "Cactus Comidas para llevar",
    "contactName": "Patricio Simpson",
    "contactTitle": "Sales Agent",
    "address": "Cerrito 333",
    "city": "Buenos Aires",
    "postalCode": "1010",
    "country": "Argentina",
    "phone": "(1) 135-5555",
    "fax": "(1) 135-4892"
}

But Ember Data sends a JSON like this:

{
    "customer": 
    {
        "id":"CACTU",
        "companyName": "Cactus Comidas para llevar",
        "contactName": "Patricio Simpson",
        "contactTitle": "Sales Agent",
        "address": "Cerrito 333",
        "city": "Buenos Aires",
        "postalCode": "1010",
        "country": "Argentina",
        "phone": "(1) 135-5555",
        "fax": "(1) 135-4892"
    }
}

How I can delete "customer" root element when sending POST/PUT operations?


回答1:


You'll want to override one of the serialize methods, I think serializeIntoHash might work:

App.CustomerSerializer = DS.RESTSerializer.extend({
  serializeIntoHash: function(hash, type, record, options) {
    Ember.merge(hash, this.serialize(record, options));
  }
});

This is instead of the normal serializeIntoHash which looks like this:

  serializeIntoHash: function(hash, type, record, options) {
    hash[type.typeKey] = this.serialize(record, options);
  }

Additional documentation can be found here:

https://github.com/emberjs/data/blob/v2.1.0/packages/ember-data/lib/serializers/rest-serializer.js#L595



来源:https://stackoverflow.com/questions/19571025/delete-json-root-element-for-post-put-operations-in-ember-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!