返回具有计算属性的 Promise

Returning a Promise with computed properties

我需要从计算的 属性 中的另一个 ember 数据模型中获取一个值。我所有的 belongsTohasMany 关系都是 async : true。我可以访问该值,但是,当我知道结果实际上解析为来自调试器的正确值。我觉得是我对promises的理解不够。

displayName : function() {
    return this.get('name') + ' ('+ this.get('currentGroup') + ')'; // currentGroup is displaying as '[Object, Object]' as it is pointing to the Promise I think
}.property('name'),

currentGroup : function() {
    var promise = this.get('groups').then(function(groups) { //groups is async : true

       //Do a bunch of complicated logic to work out a group based
       //on the effective start date

        return currentGroup.get('name'); // this resolves correctly in the debugger
    });

    return promise;
}.property()

currentGroup 将 return 一个你写的承诺,所以你应该那样做:

this.get('currentGroup').then(function(name) {
  // note you have the name you returned in the promise's callback
})

所以你可以有一个观察者而不是 displayName 属性:

displayName: null,
displayNameUpdate : function() {
    this.get('currentGroup').then(function(name) {
      var displayedName = this.get('name') + ' ('+ name + ')';
      this.set('displayName', displayedName);
    }.bind(this));
}.observes('name', 'currentGroup'),