在Ember Data中删除POST / PUT操作的JSON根元素 [英] Delete JSON root element for POST/PUT operations in Ember Data
本文介绍了在Ember Data中删除POST / PUT操作的JSON根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个Web服务,在POST / PUT动词中期望像这样的JSON:
{
/ pre>
id:CACTU,
companyName:Cactus Comidas para llevar,
contactName:Patricio Simpson,
contactTitle:Sales Agent
地址:Cerrito 333,
city:Buenos Aires,
postalCode:1010,
country
phone:(1)135-5555,
fax:(1)135-4892
}
但是,Ember Data发送这样的JSON:
{
customer:
{
id:CACTU,
companyName:Cactus Comidas para llevar,
contactName Patricio Simpson,
contactTitle:销售代理,
地址:Cerrito 333,
city:Buenos Aires,
postalCode :1010,
国家:阿根廷,
电话:(1)135-5555,
传真:(1) 135-4892
}
}
我如何删除客户发送POST / PUT操作时的根元素?
解决方案你将要覆盖一个序列化方法,我认为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);
}
其他文档可以在这里找到:
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?
解决方案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:
这篇关于在Ember Data中删除POST / PUT操作的JSON根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文