Ember.js Ember 2简单多态关系
我有一个notes模型,我想附加到其他两个模型之一,客户和供应商 在我的数据库中,我有一个foreignType和Foreigned字段,它保存客户或供应商的类型和相应的ID,如Ember.js Ember 2简单多态关系,ember.js,polymorphism,Ember.js,Polymorphism,我有一个notes模型,我想附加到其他两个模型之一,客户和供应商 在我的数据库中,我有一个foreignType和Foreigned字段,它保存客户或供应商的类型和相应的ID,如 notes: { {id: 1, body:'bar',foreignType:'customer',foreignId:100}, {id: 2, body:'foo',foreignType:'supplier',foreignId:100} } 也就是说,可以将注释附加到客户或供
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扩展customer/supplier,并拥有一个属性
,这意味着notes中的belongsTo不是多态的,因为没有任何派生类,只有noteable本身。不确定ember(数据)是否能够正确处理此嵌套notes:belongsTo('noteable')
- 从可注意扩展。如果我想拥有其他与客户或供应商相关的地址或联系人,该怎么办
- 创建重复模型,如customernote/suppliernote、customercontact/suppliercontact、customer/supplier/employee address。并让后端根据端点返回筛选的表/模型名称。不过我不想重复我自己的话
余烬数据:2.2.1我喜欢余烬文档在这里解释多态性的方式- 因此,首先您需要有一个“类型”,它将定义要使用的模型(您的数据称之为foreignType) 接下来,您的笔记模型将是多态模型(类似于上面示例中的paymentMethod模型)。如果您需要更多的澄清,请在评论中告诉我,但我认为如果您遵循给定的示例,就会非常清楚