-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Known belongsTo ids cannot use a link in a RESTSerializer #4292
Comments
It looks like this is currently the actual expected behavior. As a workaround you can use the |
@wecc Do you remember why we made it work this way? |
IIRC the original reason why local data has precedence over links is so that As I'm typing this I kind of want to remember that this (WIP test) is related... we need to be smarter with the |
@jevanlingen another workaround is to update the // app/serializers/file.js
export default DS.RESTSerializer.extend({
normalize(typeClass, hash) {
if (hash.type) {
hash.links = {
type: `/returns-file-type-from-other-api/${hash.type}`
}
// delete hash.type so the type relationship is only specified via
// the link (which is added above)
delete hash.type;
}
return this._super(...arguments);
}
}); |
@wecc based on what you are saying it sounds like it should be possible for Ember Data data to use |
@bmac yeah, it should be possible. I'm trying to find the code I did to make that test I linked pass, I suspect it should solve this issue too, but no luck :( |
Thanks for all the comments, for now I will use latest workaround @pangratz mentioned. I am still looking forward to a final solution from Ember Data Team :). |
I've been looking into this during our week here in 🇬🇷 and below is pretty much my suggestion to how this should work. It would probably introduce som backwards-incompatible changes so there's that to consider as well. Facts
belongsToProposed priority:
Pseudo code: if (relationship.resourceIdentifier) {
if (store.hasResource(relationship.resourceIdentifier)) {
return store.peekRecord(relationship.resourceIdentifier)
}
if (relationship.relatedLink) {
return adapter.findBelongsTo(relationship.relatedLink)
}
return adapter.findRecord(relationship.resourceIdentifier)
} else {
if (relationship.relatedLink) {
return adapter.findBelongsTo(relationship.relatedLink)
}
return null
} hasManyProposed priority:
Pseudo code: if (relationship.resourceIdentifiers) {
resourcesInStore = store.peekRecords(relationship.resourceIdentifiers)
if (diff(resourcesInStore, relationship.resourceIdentifiers).length == 0) {
return resourcesInStore
}
if (relationship.relatedLink) {
return adapter.findHasMany(relationship.relatedLink)
}
return adapter.findRecord(diff(resourcesInStore, relationship.resourceIdentifiers))
} else {
if (relationship.relatedLink) {
return adapter.findBelongsTo(relationship.relatedLink)
}
return null
}
|
What's the status of this? I think I've hit it in #5037 |
In a 'normal' situation there are two possibilities to obtain a belongsTo relation when using the RESTSerializer:
/name-of-model-plural/{:id}
.links
object in the normalize function of own created serializer to obtain the data. Ember will fetch the model by given link.So far, so good. But something goes wrong if both the relation id is available in the response and
links
object is also setted in your own created serializer.Ember does still call
/name-of-model-plural/{:id}
, instead of calling/own-given-link/{:id}
.See Ember Twiddle for a live example.
The text was updated successfully, but these errors were encountered: