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

Mer*_*rin 15 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"
}
Run Code Online (Sandbox Code Playgroud)

但是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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Kin*_*n2k 16

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

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

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

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

其他文档可在此处找到:

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