jeudi 4 avril 2019

Ember.js record.save() is successful, but object has state inflight

I am using a fairly new version of Ember (Ember 3.7.2, Ember Data: 3.7.0)

I create an object in the client and I create a GUID as the ID. I save it to the backend with record.save() and I use the Json Api Adapter. 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. This ID is not used to anything. The object in the client keeps it GUID 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 deleteRecord on while in state root.loaded.created.inFlight.

I am using a standard version of the JSONAPISerializer. 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?




Aucun commentaire:

Enregistrer un commentaire