Hi guys,
I’m facing a completely twisted issue right now.
When I create a new model and then persist it to my API, ember-data doesn’t handle server’s responses and therefore, ignore the ID that has been provided.
I made a lot of investigation around this, here’s what I found at the moment of writing :
Only happen if the creation process is the first save()
. That mean that if I edit something, save it, and then I create my very same object : everything is perfect.
Also, if I create several objects (even of different model class), only the first 1 will be missing his reload.
I can assure that the JSON response is always valid. If I create an object 2 times with the same data, the second one is reloaded correctly. Even if my Chrome Devtool shows me that it actually received 2 ‘201’ responses with correctly formatted data.
This post sort of complete the try I made on StackOverflow yesterday (some code is available there) : HERE
Thanks a lot in advance… this is a critical issue and I cant get forward in my project until it’s not fixed…
MASSIVE UPDATE
I was sure the problem wasn’t on my side, so I decided to search in ember-data beta 7 itself and found my edge case.
The function extractSingle
, which appears to be used to load the server’s response, returned undefined
in my error case and returned the proper data in the following saves (as I said, I only get my problem on the first save. So good so far, I found in which part of Ember-Data my problem is.
But what is it ? those lines [3021 - 3025] :
// legacy support for singular resources
if (isPrimary && Ember.typeOf(payload[prop]) !== "array" ) {
primaryRecord = this.normalize(primaryType, payload[prop], prop);
continue;
}
In my error case, the if
evaluation returns false
because of
isPrimary = type.typeKey === primaryTypeName;
When I logged those 2 values I got category
and Category
** So, TL;DR **
If the first model.save()
that is triggered is via a createdRecord, the primaryTypeName
is capitalized.
I fixed it by doing a non case-sensitive comparison
isPrimary = type.typeKey.toLowerCase() === primaryTypeName.toLowerCase();
Any thoughts about where this first capitalization may come from ? If the case don’t have no impact on this evaluation, maybe it would be a good fix for the next beta release to prevent futures headaches.