1

I am having trouble defining a computed property, based on some async associations, in the object controller. I based my example on some examples I found here and in the Ember docs.

I have three (relevant) models: A Space with one or more Subscription(s), which each have a User. They are all loaded async:

MyApp.Space = DS.Model.extend({
  name: DS.attr('string'),
  subscriptions: DS.hasMany('subscription', { inverse: 'space', async: true })
});

MyApp.Subscription = DS.Model.extend({
  space: DS.belongsTo('space', { inverse: 'subscriptions', async: true }),
  user: DS.belongsTo('user', { async: true })
});

MyApp.User = DS.Model.extend({
  name: DS.attr('string')
});

I try to compute a property mySubscription, which gets the subscription belonging to me from subscriptions, in the controller for the space because that is where I have access to the current user through a mixin(not particularly relevant for this example).

MyApp.SpaceController = Ember.ObjectController.extend(
  MyApp.CurrentUserMixin, {

  mySubscription: function () {
    var me = this.get('currentUser');
    var subscriptions = this.get('model.subscriptions');
    return subscriptions.findBy('user', me);
  }.property('model.subscriptions.@each')
});

This property always turns out to be undefined however I try it. I've tried adding .content to everything async, I tried finding by id and debugged and inspected the hell out of it. Somehow, I am not able to find anything within the array of associated objects. Anybody got an idea how I should proceed?

2
  • Could you define the subscription association on the user? Feb 19, 2015 at 13:07
  • Technically, I could, but a user might hold subscriptions to multiple spaces so we would end up at the same point I think (having to search the user subscriptions for a specific space..)
    – M Jacobs
    Feb 19, 2015 at 13:27

2 Answers 2

2

The problem is indeed, as Dom Christie points out, the async / sync mismatch. Better than returning in the resolving of the promise is setting a property with an observer like this:

updateMySubscription: function () {
  var self = this;
  var subscriptions = this.get('subscriptions').then(function (subscriptions) {      
    var mySubscription = subscriptions.findBy('user', self.get('currentUser'));
    self.set('mySubscription', mySubscription);
  });
}.observes('subscriptions.@each')

This way the mySubscription property itself becomes a normal sync property.

2
  • Great! This alternative approach fixes the problem.
    – M Jacobs
    Feb 20, 2015 at 10:05
  • It may be worth setting mySubscription: null in the controller, to prevent mySubscription from being set on the model Feb 23, 2015 at 10:25
0

model.subscriptions returns a promise, so something like the following might work:

MyApp.SpaceController = Ember.ObjectController.extend(
  MyApp.CurrentUserMixin, {

  mySubscription: function () {
    var _this = this;
    return this.get('subscriptions').then(function (subscriptions) {
      return subscriptions.findBy('user', _this.get('currentUser'));
    });
  }.property('subscriptions.@each', 'currentUser')

});

(model is omitted since you are within an ObjectController)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.