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

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

问题描述

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



如果一个用户有很多追随者(其他用户),那么将这个数据结构建立到ember数据中的最简单的方式是什么?

解决方案

我们可以找到没有疯狂的最佳方式是代理与关系对象的自联系关系,然后将其映射到用户。



所以如果用户有很多用户,那么你可以这样做:

 code> App.User = DS.Model.extend 
name:DS.attr('string')
如下:DS.hasMany('App.Follow')
关注者: ( - >
@get('follow')。map((data) - > App.User.find(data.get('followUserId')))
).property(' 。@每个')

App.Follow = Ds.Model.extend
用户:DS.belongsTo('App.User')
followUserId:DS.attr('string ')

希望有帮助!


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?

解决方案

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!

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

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