Skip to content Skip to sidebar Skip to footer

Ember Async Computed Property Returns Undefined

I am attempting to set a property to the value of a model's async, hasMany relationship. But I'm not able to return any value within the then function. App.Athlete = DS.Model.exten

Solution 1:

You aren't returning anything to the computed property, you are returning something to the callback function which is handed off to any chained promises, but that still wouldn't help had you returned the promise, since returning a promise to a computed property doesn't automatically resolve the promise and use the result at the value of the computed property.

Your current computed property is essentially doing this:

time: function() {
    var raceid= '1';
    this.get('times').then(function(times) {  // returns undefined in this scopeconsole.log(times.get('length'));     // consistently displays
        times.forEach(function(time) {
            time.get('race').then(function(timerace) {
                if(timerace.get('id')===raceid) {
                    console.log('match');     // consistently diplaysconsole.log(time.get('time')); // consistently displaysreturn time.get('time'); // returns undefined/not at all
                }
            });
        });
    });
  // I'm not returning anything soreturnundefined;
}.property('time.@each.time'),

In this situation it's easiest to use an observer and set the property (although it looks like you were accidentally watching time, instead of times)

time: null,
timWatcher: function() {
    var raceid= '1',
        self = this;
    this.get('times').then(function(times) {  // returns undefined in this scopeconsole.log(times.get('length'));     // consistently displays
        times.forEach(function(time) {
            time.get('race').then(function(timerace) {
                if(timerace.get('id')===raceid) {
                    console.log('match');     // consistently diplaysconsole.log(time.get('time')); // consistently displays
                    self.set('time', time.get('time')); // set property to time value
                }
            });
        });
    });
}.observes('times.@each.time'), //times, not time

Post a Comment for "Ember Async Computed Property Returns Undefined"