多态具有多个和属性关系在ember-data rev 12 [英] polymorphic hasMany and belongsTo relationships in ember-data rev 12

查看:85
本文介绍了多态具有多个和属性关系在ember-data rev 12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用ember-data rev12来实现我所了解的多态关系。

I am having trouble implementing what I understand to be a polymorphic relationship using ember-data rev12.

我有以下型号:

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 am trying to create a new list from the project router like so.

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

但是对服务器的请求是错误地设置多态密钥。

But the request to the server is setting the polymorphic keys incorrectly.

我期待有效载荷看起来像:

I was expecting the payload to look like:

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

但是得到:

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

我缺少什么?有没有办法定义多态类型或密钥?

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

这是我的临时黑客

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

推荐答案

首先,根据目前的型号,您不需要使用多态关联。您可能不想在关系的两端设置多态选项。

First thing, based on the current models you have, you don't need to use polymorphic associations. And you may not want to set the polymorphic option on both end of the relationship.

如果您想要您的有效载荷包含 listable ,您只需要重命名您的属性:

If you want to have 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 })

更新

根据我对你的类的理解,它是一个可以属于不同类型的 List 。所以你应该这样定义你的模型:

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

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 })

,有效内容将如下所示: / p>

and the payload will look like this:

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

我不知道你是否也希望将列表同时添加到几个列表中。根据您的模型的名称,我很想相信这不是你想要的:列表应该包含不同的对象(项目,提案,员工)。而不是项目可以有多个列表。

I don't know if you also want a list to be added to several listables at the same time. Based on the names of your models, I'm tempted to believe that it's not what you want: a list should contain different objects (project, proposal, employee). and not a project can have multiple lists.

这篇关于多态具有多个和属性关系在ember-data rev 12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆