Working with polymorphic relationships in Ember.js

This is a bit complicated for me as it is not described at all in the documentation.

I have four models:

App.Dog = DS.Model.extend();
App.Cat = DS.Model.extend();

App.Food = DS.Model.extend();
App.Toy = DS.Model.extend();

Both dogand cathave lots foodand lots toys. On the other hand, it is one instance foodand toycan belong to only one dogor cat(so this is the relation hasMany-ish on the animal side and belongsTo-ish on the element side).

Any tips on how to do this in Ember using polymorphic relationships? Or maybe there is a better approach to achieve this other than polymorphic relationships?

Also, what code should be implemented in my controller to create a new record foodor toythat belongs to dogor cat?

Thank!

+3
1

-, - :

App.Animal = DS.Model.extend({
  food: DS.hasMany('App.Food', { polymorphic: true }),
  toys: DS.hasMany('App.Toy', { polymorphic: true })
});
App.Dog = App.Animal.extend(/* .... */);
App.Cat = App.Animal.extend(/* .... */);

App.Food = DS.Model.extend({
  owner: DS.belongsTo('App.Animal', { polymorphic: true })
});
App.Toy = DS.Model.extend({
  owner: DS.belongsTo('App.Animal', { polymorphic: true })
});
+1

All Articles