How Do I Set a Value on an Associated (belongsTo) Record?


#1

Hi,

I’m probably missing something really obvious, but I can’t figure it out …

I have 2 models:

Session & Content

Content “belongsTo” Session, so each Session instance has a “content” property. The relationship is async.

What I’m trying to do is this:

When I navigate to the session route, I want to set the value of a property on the session’s content. It seems, however, that the content is never available to have anything set, though.

Using the Ember Chrome debugger, I can see that the session object does, in fact, have a content property. When I click on it, it shows that it’s a PromiseObject. Clicking further eventually gets me to the content object itself.

So I know that because it’s loaded async, it won’t be available immediately.

My question is: How or when do I know that I have the actual, resolved content object loaded so that I can set one of its properties?

Sorry if this is unclear. I can provide more information if necessary.

Thanks, Matt


Update belongsTo relation from dropdown (ember select view)?
#2

You just need to use the then method of the promise. It takes two parameters: the first is a function to execute if the data is successfully fetched, the second is an error function.

foo.get('relationship').then(function(value) {
    value.set('key', 'value');
}, function(error) {
    console.log('The promise failed');
    console.log(error);
});

#3

Thanks Gordon!

I came to the same conclusion and was about to reply to myself. Appreciate the help!

Matt