Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-Join with Ember-Data

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?

like image 939
Han Avatar asked Dec 05 '12 16:12

Han


1 Answers

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!

like image 185
Andre Malan Avatar answered Oct 10 '22 04:10

Andre Malan