删除Ember Data中POST / PUT操作的JSON根元素

时间:2013-10-24 16:05:17

标签: json ember.js ember-data

我正在使用一个在POST / PUT谓词中需要JSON的Web服务:

{
    "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"
}

但是Ember Data会发送一个这样的JSON:

{
    "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"
    }
}

如何在发送POST / PUT操作时删除“customer”根元素?

1 个答案:

答案 0 :(得分:16)

你想要覆盖其中一个序列化方法,我认为serializeIntoHash可能有效:

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

这不是正常的serializeIntoHash,它看起来像这样:

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

可以在此处找到其他文档:

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