Polymorphic hasMany and belongs to relationships in ember-data rev 12

I am having problems implementing what I understand as a polymorphic relation using ember-data rev12.

I have the following models:

App.Project = DS.Model.extend lists: DS.hasMany('App.List', { polymorphic: true }) App.Proposal = DS.Model.extend lists: DS.hasMany('App.List', { polymorphic: true }) App.Employee = DS.Model.extend lists: DS.hasMany('App.List', { polymorphic: true }) App.List = DS.Model.extend name: DS.attr('string') #project: DS.belongsTo('App.Project', { polymorphic: true }) 

And I'm trying to create a new list from the project router.

 App.ProjectRoute = Ember.Route.extend events: newList: (project) -> lists = project.get('lists') list = App.List.createRecord(name: 'list1') lists.pushObject(list) @store.commit() 

But a server request does not correctly set polymorphic keys.

I expected the payload to look like this:

  { list: { name: list1, listable_type: project, listable_id: 100 } } 

But received:

 { list: { name: list1, project_type: project, project_id: 100 } } 

What am I missing? Is there a way to define a polymorphic type or key ?.


Here is my temporary hack

https://gist.github.com/arenoir/5409904

+1
source share
1 answer

First of all, based on existing models, you do not need to use polymorphic associations. And you may not want to set the polymorphic parameter at both ends of the relationship.

If you want your payload to contain listable , you just need to rename your attribute:

 App.List = DS.Model.extend name: DS.attr('string') listable: DS.belongsTo('App.Project', { polymorphic: true }) 

UPDATE

Based on my understanding of your classes, this is a List that can belong to different types. Therefore, you should define your models as follows:

 App.Listable = DS.Model.extend lists: DS.hasMany('App.List') App.Project = App.Listable.extend App.Proposal = App.Listable.extend App.Employee = App.Listable.extend App.List = DS.Model.extend name: DS.attr('string') listable: DS.belongsTo('App.Listable', { polymorphic: true }) 

and the payload will look like this:

 {list: {name: "list1", listable_type: 'project', listable_id: 100}} 

I do not know if you also want the list to be added to several lists at the same time. Based on the names of your models, I am inclined to believe that this is not what you want: the list should contain different objects (project, proposal, employee). not a project can have multiple lists.

+6
source

All Articles