Ember数据多态关联

dag*_*da1 13 ember.js ember-data

有没有人想出多态关联和余烬数据的答案?

我们需要一些能够从我所知道的方式查询关系另一端的类型的方法.

有人对此有何看法?

Wil*_*Wit 8

使用最新的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})
});
Run Code Online (Sandbox Code Playgroud)

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

DS.RESTAdapter.configure('App.Post' {
  alias: 'post'
});
DS.RESTAdapter.configure('App.Comment' {
  alias: 'comment'
});
Run Code Online (Sandbox Code Playgroud)

您服务器的预期结果应如下所示:

{
    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"
    }
}
Run Code Online (Sandbox Code Playgroud)

这个github线程中的更多信息


And*_*lan 3

所以我有东西。它还没有完成,或者完全干净,但它可以工作。基本上,我使用 mixin 来完全绕过 Ember 关联。我确信这可以被整合到适配器或商店中,但目前这是可行的。

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

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

我将 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')
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)

控制器层受到保护,免受所有这些混乱的影响,除了必须调用 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()
Run Code Online (Sandbox Code Playgroud)

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