如何定义Ember数据中异步关联的计算属性



我在对象控制器中定义基于某些异步关联的计算属性时遇到问题。我的例子基于我在这里和Ember文档中找到的一些例子。

我有三个(相关的)模型:一个Space和一个或多个Subscription(s),每个模型都有一个User。它们都加载了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')
});

我尝试在空间的控制器中计算属性mySubscription,它从subscriptions获得属于我的订阅,因为这是我可以通过mixin访问当前用户的地方(与本例无关)。

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')
});

无论我怎么尝试,这个属性总是undefined。我尝试过将.content添加到所有async中,我尝试过通过id进行查找,并对其进行了调试和检查。不知何故,我在关联对象的数组中找不到任何东西。有人知道我该怎么做吗?

正如Dom Christie所指出的,问题确实是异步/同步不匹配。在解决承诺时,最好与这样的观察者设置一个属性:

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')

这样,mySubscription属性本身就变成了一个正常的同步属性。

model.subscriptions返回一个promise,因此以下内容可能有效:

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')
});

(由于您在ObjectController中,因此省略了model