2015-06-04 42 views
0
DEBUG:    Ember      : 1.8.1 
ember.js:15373DEBUG: Ember Data    : 1.0.0-beta.18 
ember.js:15373DEBUG: Handlebars    : 1.3.0 
ember.js:15373DEBUG: jQuery     : 1.11.3 

所以我有一個「請求」模型,它可以取決於帳戶或其他請求,當它滿足時代表和帳戶。JSON對於Ember數據多態關係的期望

我有點困惑於如何獲得適合Ember使用的JSON。

在服務器端,ActiveModelSeriailzers似乎想序列化這樣的多態關係:

{ 
    "id" : 1, 
    "account_id" : { "id": 1, "type": "account"} 
} 

我有以下的ED型號:

// abstract-account.js 
export default DS.Model.extend(); 

//account.js 
export default AbstractAccount.extend(); 

//request.js 
export default DS.Model.extend({ 
    account: DS.belongsTo('abstractAccount', { polymorphic: true}) 
}); 

當我嘗試使用加載記錄默認json,我得到

Error: Assertion Failed: You must include an `id` for account in an object passed to `push` 

在我的閱讀中,我得到了印象離子是屬於關聯多態性assocaitions應該被髮送,如:

{ 
    "id" : 1, 
    "account_id" : 1, 
    "account_type" : "Account" 
} 

如果我更改服務器序列化那樣,我反而得到

Uncaught Error: Assertion Failed: You may not pass `null` as id to the store's find method 

我真的不知道我錯過了什麼,或目前推薦的做法是什麼。

回答

1

我正在使用與第一個約定類似的設置。請記住灰燼預期下的類名來命名空間JSON對象,所以對於一個GET到/請求您將需要返回類似

{ 
    'requests': [ 
     { 
      'id': 1, 
      'account': { id: 10, type: 'account' } 
     }, 
     { 
      'id': 2, 
      'account': { id: 20, type: 'some_other_abstract_account' } 
     } 
    ] 
} 

這與RESTAdapter應用/ adapter.js宣佈,我還沒有嘗試過ActiveModelAdapter。我看到文檔建議another convention,但它的工作與我的燼數據1.0.0-beta.16

+0

我得到它的工作時,我做了兩件事:1.停止另一個串行器發送太多sideloading記錄,這是搞亂我的新動作的魔咒,2.停止使用AMS多態關係和建立像你張貼自己的JSON。 – DVG