3
votes

I have a belongsTo relationship defined in the model, e.g.

gallery: DS.belongsTo('store-gallery', { async: true }),

Later on, I'll create a new object and assign it to the relationship if it is null:

let galleryObject = this.get('store').createRecord('store-gallery', {});
this.set('gallery', galleryObject);
console.log('Created', galleryObject, await this.get('gallery'));

The problem is that the this.get('gallery') call returns NULL due to Ember reloading the relationship.

How do I set the relationship (preferrably without having to convert it to non-async) without it immediately overwriting it?

1

1 Answers

1
votes

I did not give what you asked a try and to be honest I am a little bit surprised that this works the way you explained. I would not have expected ember-data to reload the relationship; because you are not forcing a reload on the relation within the code block you provided.

Aside from what I have just explained; you can always get the current value of the relationship as follows; where this is the owner of the relationship as in your example:

this.belongsTo('gallery').value()

This will provide you to reach the current value of the relationship even if it is async in a sync way. So you do not even need to have the await there. See the official API documentation to see what I mean. Hope this helps.