I am using a fairly new version of Ember (Ember 3.7.2, Ember Data: 3.7.0)
I create a record in the client (using store.createRecord()) and I give it a GUID as the ID.
The record must have an ID in the client because it is not immediately saved to the backend. The user may edit the record before it is saved.
I then save it to the backend with record.save() and I use the JSONAPIAdapter.
The backend changes the ID to an integer (An autoincrement in the database) and the payload returned from the server includes the newly saved object with this new ID.
The idea is that the ember data framework shall update the record with the new ID from the backend. That does not happen in ember-data 3.7. It did happen in ember-data 3.0.1 before I upgraded to the latest ember version.
The object in the client keeps it GUID and furthermore and it now has the state "isSaving"=true.
So, if I later try to delete this object I get this error:
Attempted to handle event
deleteRecordon while in state root.loaded.created.inFlight.
I am using a standard version of the JSONAPISerializer.
To be able to give the record an client-side ID, I have subclassed the JSONAPIAdapter to include this mehtod:
generateIdForRecord: function(/* store, record */) {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c==='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
What should I do to save properly and don't get an error when I try to delete the object?
NOTE
I rolled back to ember version 3.0.0 / ember data version 3.0.1. In that version it worked fine. I.e. the ID was updated in the record when the save call returned from the server with the new id.
So there must be a change from ember 3.0.0 to 3.7.0 related to record.save and update of ID and state isSaving/saved.