2013-10-24 57 views
15

我消費,在POST/PUT動詞預計這樣的JSON Web服務:刪除JSON根元素數據

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

但灰燼數據發送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」根元素?

+0

看看這個線程http://stackoverflow.com/questions/17429426/ember-js-rest-adapter-without-json-root – melc

+0

@melc似乎只涉及GET請求 – Merrin

回答

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

+0

這很容易,我很慚愧。非常感謝您 – Merrin

+0

嗯......對我來說不是那麼多......我在序列化程序中做了完全相同的事情,但調用_super()的結果是一個已經變平的對象,沒有根節點。恐怕這種技術不起作用。 –

+0

我實際上沒有對Steve進行測試,但它應該在那裏,它可能實際上是serializeIntoHash方法而不是serialize方法,參見上文。 – Kingpin2k