默認情況下,當RESTAdapter向服務器發送一個請求,POST數據,它發出的模型typeKey
作爲哈希的根:Ember-Data:我的自定義`serializeIntoHash`返回一個空的散列?
typeKey: { data }
,但我的服務器需要一個「無根」的哈希:
{ data }
我發現這是覆蓋的方法,但我正在做的事情不僅導致根被刪除,而且散列本身也是空的......即使我的console.log顯示record
正在序列化到哈希。
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
serializeIntoHash: function(hash, type, record, options) {
console.log(' hash going in: ' + JSON.stringify(hash)); // hash is {} going in
hash = this.serialize(record, options);
console.log('hash going out: ' + JSON.stringify(hash)); // hash is { full of data } going out
return hash; // after this, for some reason the request goes out as an empty hash {}
}
});
我是不是正確地返回修改後的散列?我也試過這些變化:
return (hash, type, record, options)
和
return this._super(hash);
和
return this._super(hash, type, record, options);
沒有我回似乎工作。我不知道我做錯了什麼?
我注意到在API Docs for the method,沒有return
使用,但如果我排除,我得到完全相同的問題,所以我不知道我是否甚至需要返回或沒有?
好的,這是有道理的。所以,如果我理解你,我需要單獨獲取'serialize(record,options)'的結果,並以某種方式循環並將結果中的屬性添加到散列中的鍵中? – Grapho 2014-10-02 16:14:42
或...實際上將hash和serialize()的結果合併,就像在共享的鏈接中一樣? ...聽起來更容易 – Grapho 2014-10-02 16:24:41