2016-03-10 112 views
1

我試圖用Ember和Node創建一個記錄。顯然,Ember喜歡嵌套你的物體。Ember createRecord/RESTAdapater沒有嵌套

如果我給它:

this.store.createRecord('meal', { 
    name: 'the name', 
    meal_time: 'the time', 
    tolerance: 'ouch' 
    }).save(); 

節點獲得這req.body

{ meal: 
    { id: 'e5c90c23-1eb1-49e1-a3fa-865944b5eeea', 
    name: 'the name', 
    meal_time: 'the time', 
    tolerance: 'ouch' } 
} 

我寧願修改餘燼側比節點側。

有沒有辦法讓createRecord發送對象而不嵌套它?

+0

多數民衆贊成在多餘的作品!它試圖考慮使用REST資源來保存模型。另外從REST Api的角度來看,這是正確的。 – AnujKu

回答

0

實現這一目標的最簡單的方法是壓倒Serializer.serializeIntoHash() http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_serializeIntoHash

serializeIntoHash(hash, type, snapshot, options) { 
    Ember.merge(hash, this.serialize(snapshot, options)); 
} 

這個代碼將所有傳出的模型刪除頂層對象。

您可以在您的應用程序序列化器或任何您可能具有的模型特定的序列化器上覆蓋它。

2

您需要自定義您的序列化程序。

創建一個像這樣的應用程序序列化程序:ember g serializer application

然後編輯它以使用基本RESTSerializer

然後通過覆蓋其屬性和方法使其產生所需的輸出來定製它。

即時定製方法是normalizeCreateRecordResponse,但這只是表面。

理想情況下,您希望各種操作的一致行爲,而不僅僅是createRecord。因此,您需要研究串行器的組織方式。閱讀official guide to serializers,引導至JSONSerializerRESTSerializer,查看RESTSerializer代碼庫。