Polymorphic Has Many Relationship


#1

I just stumbled on to Ember Data Polymorphic Relationships. I think they would be useful for what I am working on. Here is my main model. This model defines a has many relationship with topics thats both async and polymorphic.

    App.Source = DS.Model.extend({
        name: DS.attr('string'),
        type: DS.attr('string'),
        locationType: DS.attr('string'),
        locationSpecific: DS.attr('boolean'),
        primary: DS.attr('boolean'),
        published: DS.attr('boolean'),
        sort: DS.attr('number'),
        topics: DS.hasMany('topic', {
            async: true,
            polymorphic: true
        })
    });

Next we have the Topics with a Base class being ‘Topic’

    App.Topic = DS.Model.extend({
        name: DS.attr('string'),
        sort: DS.attr('number'),
        source: DS.belongsTo('source')
    });
    
    App.RegTopic = App.Topic.extend({
        test: DS.attr('number', {
            defaultValue: 8
        }),
        notes: DS.hasMany('notes', {
            async: true
        })
    });
    
    App.SummaryTopic = App.Topic.extend({
        number: DS.attr('number', {
            defaultValue: 9
        })
    });

and here is how I call to get the topics

App.TopicsRoute = Ember.Route.extend({
     model: function() {
           return this.modelFor('source').get('topics');
     }
});

When I list the sources, I get a list of the following objects back

 {
         id: 1
         name: "test"
         type: "testType"
         locationType: "international"
         locationSpecific: "true"
         primary: true
         published: true
         sort: 1
         links: {
                 topics: "/topics?sourceId=1"
         }
    }

then my topic call gets objects like these back

{
   id: 4
   sourceId: 1
   name: Topic 4
   type: "regTopic"
   sort: 1
}

Am I missing something? Can you not use polymorphic relationships with the ‘links’ object?

From my understanding of the Polymorphic Relationship, when I make the call to /topics?sourceId=1 its should be essentially loading 2 different topics in one call so then I can display the regTopic and summaryTopic on the same page but in seperate lists and keep them as seperate objects?


#2

Shouldn’t your model include this:

links: DS.hasMany('topic', {
  async: true,
  polymorphic: true
})

instead of topics ?


#3

I don’t believe so. That links hash is a way to lazy load data without passing ID’s. Ember looks for that links hash then connects topics with the topics in my model. And that part does work, when I do

 return this.modelFor('source').get('topics');

It makes the call to go get the topics using the URL passed down (/topics?sourceId=1)

http://stackoverflow.com/questions/18812730/ember-data-lazy-load-association-with-links-attribute


#4

Well, I guess I should really re ask this questions as ‘Can you use polymorphic relationships and the links hash’?


#5

@bakerac4 I believe polymorphic relationships are supported with the links hash. All the code you’ve shown so far looks correct. Could you describe what issues you are seeing that make it seem like the polymorphic relationships aren’t working?


#6

Hey @bmac thanks for the reply!

Here is what I see in my ArrayController

Here is the list of content

I would expect this list to have App.SummaryTopic and & App.RegTopic ?

Also in the console I see ‘WARNING: The payload for ‘App.Topic’ contains these unknown keys: [type]. Make sure they’ve been defined in your model.’ which if it was working correctly It shouldn’t be complaining about.

Some other possibly pertinent information: Im using Ember v1.9.0 and ember-data v1.0.0~beta.12 and the the DS.RESTAdapter


#7

I’ve seen that type warning happen before with embedded polymorphic relationships. What happens if you add a type attribute to the Topic model?


#8

The only thing that changes is that there is now a type called ‘summaryTopic’ or ‘regTopic’ in my App.Topic models.

Also I should point out that the properties defined in the models that inherit App.Topic never show up. (‘test’ or ‘number’)


#9

Here is a js-bin that I’m trying to get working. Could you take a look? http://emberjs.jsbin.com/medojitibo/1/edit?html,js,console,output . It fails on trying to get the topics for the source using the links method with polymorphic relationships.

Either that or you don’t happen to know of or have a working js bin?


#10

Sorry for the delay @bakerac4. I worked with that jsbin you provided and I think I was able to get it working check out my updates here: http://emberjs.jsbin.com/litoda/1/edit?html,css,js,output

It looks like async polymorphic relationships need a type parameter on the model. Also attempting to get the topics relationship before the source model was loaded seemed to be causing some issues so I moved it into the then callback.