返回具有计算属性的Promise

时间:2015-01-29 04:43:46

标签: ember.js ember-data

我需要从计算属性中的另一个ember-data模型中获取值。我的所有belongsTohasMany关系都是async : true。我可以访问该值,但是,它显示为' [对象,对象]'当我知道结果实际上从调试器解析为正确的值。我认为这是由于我对承诺缺乏了解。

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

1 个答案:

答案 0 :(得分:1)

currentGroup会返回你写的一个承诺,所以你应该这样做:

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