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 Attachements to Projects 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' });
});
Is that something officially supported by Ember?
In our project using Ember 2.4, we have few entities, Task, Assignment and Tag. Task are taggable and assignable through polymorphic associations.
This is our models structure:
// app/models/task.js
import DS from 'ember-data';
import Taggable from 'app/mixins/taggable';
import Assignable from 'app/mixins/assignable';
export default DS.Model.extend(Taggable, Assignable, {
});
// app/models/tag.js
import DS from 'ember-data';
export default DS.Model.extend({
taggable: DS.belongsTo('taggable', { polymorphic: true }),
});
// app/models/assignment.js
import DS from 'ember-data';
export default DS.Model.extend({
assignable: DS.belongsTo('assignable', { polymorphic: true }),
});
// app/mixins/taggable.js
import Ember from 'ember';
import DS from 'ember-data';
export default Ember.Mixin.create({
tag: DS.belongsTo('tag'), // you can go with hasMany here, we only have one-to-one association
});
// app/mixins/assignable.js
import Ember from 'ember';
import DS from 'ember-data';
export default Ember.Mixin.create({
assignment: DS.belongsTo('assignment'), // you can go with hasMany here, we only have one-to-one association
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With