Ember data model inheritance in DS.hasMany


Anyone have any ideas on how to implement this correctly?

I currently have a model with a nested hasMany structure and one of my hasMany relationships is a base model that is being extended to define many different versions of the base model.


Group = DS.Model.extend({
    project: DS.belongsTo('project'),
    name: attr(),
    requirements: DS.hasMany('requirement', {'async': true})

Requirement = DS.Model.extend({
    type: attr(),
    name: attr(),
    group: DS.belongsTo('group')

ComputeRequirement = Requirement.extend({
    type: attr({default: 'compute_requirement'}),
    platform: attr(),
    compute: attr(),
    memory: attr(),
    minInstances: attr()

NetworkRequirement = Requirement.extend({
    type: attr({default: 'network_requirement'}),
    inboundTrxSize: attr(),
    inboundTrxRate: attr(),
    outboundTrxSize: attr(),
    outboundTrxRate: attr()

The problem that I seem to be having is that when ember data loads this relationship from the server it is only defining these as requirement models and the only data that i receive is type and name.

Is there a preferred way of doing this? Or is this even supported at all?


You should define your group requirement relationship as polimorphic.

Group = DS.Model.extend
    requirements; DS.hasMany('requiremnets', {async: true, polymorphic: true}

and your server should return a JSON of this style:

    id: '1',
    requirements: [{id: 1, type: 'compute_requirements'}]


Thank you very much the polymorphic relationship works fantastically