9
votes

I'm trying to get Ember Data working with a hasMany association with class inheritance.

Example in Ember code:

var Person = DS.Model.extend({
  name: DS.attr('string'),
  animals: DS.hasMany('Animal')
});

var Animal = DS.Model.extend({
  name: DS.attr('string'),
  owner: DS.belongsTo('Person')
});

var Dog = Animal.extend({
  isBarking: DS.attr('boolean')
});

var Cat = Animal.extend({
  isMeowing: DS.attr('boolean')
});

var fred = Person.createRecord({name: 'Fred'});
var ed = Dog.createRecord({name: 'Ed', isBarking: true});
// This gives:
// Error: assertion failed: You can only add records of Animal to this relationship.
fred.get('animals').pushObject(ed);

I would like to be able to add Cat and Dog instances to the animals for each Person.

An example of how it might be implemented is given by what Mongoid provides for Ruby/Rails:

http://mongoid.org/en/mongoid/docs/documents.html#inheritance

I'm aware that Mongoid's inheritance implementation (a custom _type attribute for each record) will probably be too specific for Ember Data to implement.

If anyone could give me a hint whether the above is possible with Ember Data, and if so, how I could implement this, it would be greatly appreciated. If this is something that won't be supported by Ember Data at all, that's also an answer I'd like to have.

I'm running Ember 1.0.0-pre4 and a custom Ember Data build from a few days ago.

1

1 Answers

2
votes

Polymorphism isn't officially supported yet, but there is a standing pull request that proposes exactly the feature you are looking for.