【问题标题】:Ember-data polymorphic associationsEmber 数据多态关联
【发布时间】:2013-04-17 17:25:25
【问题描述】:

有人想出关于多态关联和 ember-data 的答案吗?

我们需要某种方式来查询关系另一端的类型。

有人对此有什么想法吗?

【问题讨论】:

  • 你能举一个具体的例子说明你想做什么吗?
  • 我希望能够有类似的东西: JSON: { ... "attachedTo": { "user_id": 16 } ... } 并在模型定义中: attachTo: DS .belongsTo("auto")

标签: ember.js ember-data


【解决方案1】:

借助最新的 ember-data 构建,您现在可以使用多态关联:

您需要配置模型以使其具有多态性:

/* polymorphic hasMany */
App.User = DS.Model.extend({
 messages: DS.hasMany(App.Message, {polymorphic: true})
});

App.Message = DS.Model.extend({
  created_at: DS.attr('date'),
  user: DS.belongsTo(App.User)
});

App.Post = App.Message.extend({
  title: DS.attr('string')
});

/* polymorphic belongsTo */
App.Comment = App.Message.extend({
  body: DS.attr('string'),
  message: DS.belongsTo(App.Message, {polymorphic: true})
});

您还需要在RESTAdapter 上配置alias 属性

DS.RESTAdapter.configure('App.Post' {
  alias: 'post'
});
DS.RESTAdapter.configure('App.Comment' {
  alias: 'comment'
});

您的服务器预期的结果应该是这样的:

{
    user: {
        id: 3,
        // For a polymorphic hasMany
        messages: [
            {id: 1, type: "post"},
            {id: 1, type: "comment"}
        ]
    },

    comment: {
        id: 1,
        // For a polymorphic belongsTo
        message_id: 1,
        message_type: "post"
    }
}

更多信息在this github thread

【讨论】:

  • 当一个模型具有多个多态关联时,我遇到了一个问题——一个帖子可以是可评论的、可附加的和可通知的——你以前遇到过这个问题吗?我通过让每个“多态”模型都从上一个扩展来部分解决了这个问题,但这意味着我无法控制每个模型上出现的属性。
  • 碰巧知道hasMany{id: 1, type: 'type'} 对象替换ID,而belongsTo 将其注入父对象的原因?如果它们是相同的,那就更干净了,例如comment: {id: 1, message: {id: 1, type: 'post'}}
【解决方案2】:

所以我有东西。它还没有完成,也没有完全干净,但它可以工作。基本上,我使用 mixin 来完全绕过 Ember 关联。我确信这可以滚动到适配器或商店中,但现在这可行。

多态模型通过带有 itemId 和 itemType 的 JSON:

App.Follow = DS.Model.extend
  user: DS.belongsTo('App.User')
  itemId: DS.attr("number")
  itemType: DS.attr("string")

我在与之关联的模型中添加了一个 mixin:

App.Hashtag = DS.Model.extend App.Polymorphicable,
  follows:(-> 
  name: DS.attr("string")
    @polymorphicFilter(App.Follow, "Hashtag")
  ).property('changeCount')  #changeCount gives us something to bind to

  followers: (->
    @get('follows').map((item)->item.get('user'))
  ).property('follows')

mixin 实现了三种方法,一种更新changeCount,一种返回模型的类型,以及通过itemType 和id 过滤模型的polymorphicFilter 方法:

App.Polymorphicable = Ember.Mixin.create
  changeCount: 1 

  polymorphicFilter: (model, itemType)->
    App.store.filter model, 
      (data) =>
        if data.get('itemId')
          @get('id') is data.get('itemId').toString() and data.get('itemType') is itemType 

  itemType:()->
    @constructor.toString().split('.')[1]

  updatePolymorphicRelationships:()->
    @incrementProperty('changeCount')

除了必须调用 updatePolymorphicRelationship 以确保绑定触发之外,控制器层受到保护,不会出现所有这些问题:

App.HashtagController = Ember.ObjectController.extend
  follow:()->
    App.Follow.createRecord({
      user: @get('currentUserController.content')
      itemId: @get('id')
      itemType: @get('content').itemType()
    })
    #this provides a way to bind and update. Could be refactored into a didSave()
    #callback on the polymorphic model.
    @get('content').updatePolymorphicRelationships()
    App.store.commit()

这就是我目前所拥有的。我试图将东西保留在模型层中,因为它只是从适配器层移除了一步。如果看起来 Ember Data 将来根本不会考虑多态,那么将这一切提升到更高的水平是有意义的,但就目前而言,这可行并且让我的控制器(相对)干净。

【讨论】:

    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多