1
votes

I need to get hold of a value from another ember-data model in a computed property. All my belongsTo and hasMany relations are async : true.I have access to the value, however, it is displaying as '[Object, Object]' when I know the result actually resolves to the correct value from the debugger. I think it is due to my lack of understanding about 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()
1

1 Answers

1
votes

currentGroup will return a promise a you wrote, so you should rather do it that way:

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

so instead of having displayName property, you could have an observer:

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