0

I'm trying to implement a full scale friending system with Ember (with friend requests that can be accepted or rejected, the ability to cancel friend requests, and unfriending). In order to do this, I have my Rails backend pass three different friendship arrays via JSON to ember, which are captured in the following ember models (extraneous stuff removed):

App.User = DS.Model.extend
  name: DS.attr('string')
  friendships: DS.hasMany('friendship', { inverse: 'user' })
  requestedFriendships: DS.hasMany('friendship', { inverse: 'user' })
  pendingFriendships: DS.hasMany('friendship', { inverse: 'user' })

App.Friendship = DS.Model.extend
  status: DS.attr('string') # can be 'accepted', 'requested', 'pending'
  user: DS.belongsTo('user', { inverse: ??? }) # the problem is here
  friend: DS.belongsTo('user', { inverse: null })

When I go and try to write code to add/remove friendships, ember complains that I need to specify an inverse to user in the Friendship model. My problem is, it doesn't really make sense for the inverse to be defined statically as any one attribute (e.g. 'friendship'). What I really want is the inverse of friendship.user to be 'friendships', the inverse of requestedFriendship.user to be 'requestedFriendships', and the inverse of pendingFriendship.user to be 'pendingFriendships'.

I thought I could get it to work by passing in a computed property to the inverse option, where I compute which string to assign it to based on the status of the friendship. I've tried to do this:

App.Friendship = DS.Model.extend
  status: DS.attr('string') # can be 'accepted', 'requested', 'pending'
  user: DS.belongsTo('user', { inverse: @get('userInverse') })
  friend: DS.belongsTo('user', { inverse: null })

  userInverse:
    if @get('status') == 'accepted'
      'friendships'
    else if @get('status') == 'requested'
      'requestedFriendships'
    else
      'pendingFriendships'

but this fails with

Uncaught TypeError: Object [object global] has no method 'get'

I've tried variations upon this general idea, and I could not figure out how to rescope in such a way to access this within the DS.belongsTo function call.

This feels like functionality that is crucial to successfully implementing full scale social networks using ember-data, and yet I could not find any documentation on how to do this or if it's even possible. The ember.js website has very little to say about this, and in their example, they simply statically assign the inverse to be one of the possible choices. But then what about the other ones? Doesn't make much sense to me.

Bottom line: how do I contextually/dynamically/conditionally or otherwise non-statically assign the inverse of an element in an ember-data record? Or if that is truly not possible, how else would I solve this issue?

xph
  • 752
  • 5
  • 16
  • Seems like similar problem i had here : http://stackoverflow.com/questions/21269624/creating-records-of-self-referring-model-causing-error – Deewendra Shrestha Jan 29 '14 at 04:15
  • Thanks for the comment, although I don't think we have quite the same problem. In order to fix your issue, you simply had to define static inverses to your relationships. My problem here is that I don't want to set static inverses, but have the inverse depend on something instead... – xph Jan 29 '14 at 07:56

0 Answers0