Self-Join with Ember-Data

时光总嘲笑我的痴心妄想 提交于 2019-12-29 04:23:25

问题


Does anyone have any suggestions on how to manually create a self-join relationship using 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?


回答1:


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

Hope that helps!



来源:https://stackoverflow.com/questions/13727512/self-join-with-ember-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!