Han
Han

Reputation: 1293

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?

Upvotes: 8

Views: 1154

Answers (1)

Andre Malan
Andre Malan

Reputation: 2043

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!

Upvotes: 9

Related Questions