Self-Join with Ember-Data
ember-data, ember.js, javascript
Solution
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!
Problem
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?