0
votes

I have a relationship between two models as A has many B and B belongs to A. When loading A, B is sideloaded and I can use the inverse relationship B->A just fine in the template. However, when I try to use belongsTo() on a snapshot for one of the loaded B models, it returns null. Is that supposed to work and how come it works in the template but not with a snapshot in the adapter?

The code is as simple as the example here: https://guides.emberjs.com/v2.11.0/models/relationships/

As said above, it works fine in the template, but not with a snapshot in the adapter. I can also see in the debugger that inverseRecord is null on the relationship when stepping through the code for belongsTo()

EDIT:

Created a twiddle with an example of this. Check the adapter for server and the log output when pressing delete: https://ember-twiddle.com/71c98929c307284c78dae336d6cd040b. Expected is to be able to resolve the belongsTo relationship in the adapter but as seen it returns null. It can also be seen that it works fine in the template.

1
I tried doing something with serializer but serializes is clearly not the issue if it works in template...Senthe

1 Answers

0
votes

You are removing relationship before deleting record. By calling user.get('servers').removeObject(server) relationship is deleted. Afterwards you are calling server.deleteRecord() which triggers your adapters deleteRecord method. Since relationship is deleted before snapshot.belongsTo('user') returns null. Just remove servers.removeObject(server); call to see expected behavior.