我有一個筆記模型,我想附加到兩個其他模型之一,客戶和供應商。Ember 2簡單的多態關係
在我的數據庫中,我有一個foreignType和foreignId字段保存類型和客戶或供應商相應的ID,像
notes: { {id: 1, body:'bar',foreignType:'customer',foreignId:100},
{id: 2, body:'foo',foreignType:'supplier',foreignId:100}
}
也就是說,一個音符可以連接到客戶或供應商。
約定似乎是該字段被稱爲noteType? 我看到一個,其中相關類型嵌套在JSON中,而不是在根中。
我燼模型是這樣的:
//pods/note/model.js
export default DS.Model.extend({
//...
body: DS.attr('string'),
foreign: DS.belongsTo('noteable',{polymorphic:true})
});
//pods/noteable/model.js (is there a better/conventional place to put this file?)
export default DS.Model.extend({
notes: DS.hasMany('note')
});
//pods/customer/model.js
import Noteable from '../noteable/model';
export default Noteable.extend({ //derived from Noteable class
name: DS.attr('string'),
//...
});
//pods/supplier/model.js
// similar to customer
// sample incoming JSON
//
{"customer":{"id":2,"name":"Foobar INC",...},
"contacts":
[{"id":1757,"foreignType": "customer","foreignId":2,...},
{"id":1753,"foreignType": "customer","foreignId":2,...},
...],
...
"todos":
[{"id":1,"foreignType":"customer","foreignId":2,"description":"test todo"}],
"notes":
[{"id":1,"foreignType":"customer","foreignId":2,"body":"Some customer note "}]
}
如何正確設置它,即是什麼灰燼期待?
我的筆記未正確附加到客戶模型。它們顯示在Ember Inspector的「數據」選項卡中,但任何客戶的備註清單均爲空。
我可以看到幾種可能性:
從DS.Model擴展客戶/供應商,並有一個屬性
notes: belongsTo('noteable')
,這將意味着在紙幣屬於關聯並不多態的,因爲不會是任何派生類,只有注意本身。不知道如果燼(數據)可以正確處理這個嵌套。從延伸注意。如果我想擁有地址或聯繫人等其他內容,那麼這些內容可能與客戶或供應商有關?
創建像customernote/suppliernote,customercontact/suppliercontact,customer/supplier/employee地址的重複模型。並根據端點讓後端返回已過濾的表格/型號名稱。我不喜歡重複自己,雖然....
恩貝爾:2.2.0
灰燼數據:2.2.1