与mixin的多态性关系 [英] Polymorphic relationship with mixin
问题描述
在Ember-Data中使用 hasMany
和 belongsTo
关系时,是否必须指定一个类,或我可以指定一个mixin吗?例如,我有一个 Attachement
模型,我想链接到其他一些模型。具体来说,我想将 Attachement
s分配给项目
和组件
。我可以在项目
和组件
中使用mixin,并使用mixin作为逆向如下?
When using the hasMany
and belongsTo
relationships in Ember-Data, does it have to specify a class, or can I specify a mixin? For instance, I have an Attachement
model that I want to link to some other models. Specifically, I want to assign Attachement
s to Project
s and Components
. Can I use a mixin on Projects
and Component
and use that mixin as the inverse like below?
App.Attachment = DS.Model.extend({
attachedTo: DS.belongsTo('canHaveAttachments', { inverse: 'attachments'});
});
App.CanHaveAttachmentsMixin = DS.Mixin.create({});
App.Project = DS.Model.extend(App.CanHaveAttachmentsMixin, {
attachments: DS.hasMany('attachment', { inverse: 'attachedTo' });
});
Ember正式支持的是什么?
Is that something officially supported by Ember?
推荐答案
我已经花了最后一天测试和研究这个问题,我终于找到了答案:不。我做了相当多的测试,不能让它工作。最后,我发现这是因为mixin(据我所知)不在对象的原型链中。
I've spent the last day testing and researching this problem and I've finally found the answer: no. I did quite a bit of testing and couldn't get it to work. Finally, I figured out that it's because mixins (as far as I can tell) aren't in the prototype chain of an object.
为了解决问题,我只是最终使用多个关系而不是单个多态关系。它不是面向对象,但更为明确。
To solve the problem, I just ended up using multiple relationships instead of a single polymorphic one. It's not quite as object oriented, but it's more explicit.
这篇关于与mixin的多态性关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!