使用 Ember-Data 自加入 [英] Self-Join with Ember-Data

查看:25
本文介绍了使用 Ember-Data 自加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对如何使用 ember-data 手动创建自联接关系有任何建议吗?

Does anyone have any suggestions on how to manually create a self-join relationship using ember-data?

例如,如果一个用户有很多关注者(其他用户),将这种数据结构构建到 ember-data 中的最简单方法是什么?

If, for example, a user had many followers (other users), what would be the simplest way to build this data structure into ember-data?

推荐答案

我们能找到的最好的方法是使用关系对象代理自联接关系,然后将其映射到用户.

Best way that we could find without going crazy was to proxy the self-join relationship with the relationship object, then just map that to the user.

因此,如果用户通过以下方式拥有许多用户",那么您可以这样做:

So if a user has many "users" through follows then you can do:

App.User = DS.Model.extend
  name: DS.attr('string')
  follows: DS.hasMany('App.Follow')
  followers:(->
    @get('follows').map((data)-> App.User.find(data.get('followedUserId')))
  ).property('follows.@each')

App.Follow = Ds.Model.extend
  user: DS.belongsTo('App.User')
  followedUserId: DS.attr('string')

希望有帮助!

这篇关于使用 Ember-Data 自加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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