mardi 19 janvier 2016

Ember - How to deal with backend that creates resource on PUT request if resource not found?

I have a backend that will create a new resource on a PUT request if the requested resource is not found. I.E...

  1. Make a PUT request to /api/v1/person/1/

  2. For one reason or another person with id 1 has been deleted from server

  3. Server creates new person record with id 2

Now in my case these requests are being triggered from an Ember app. When I try to update a person in the Ember store with id 1 and it is deleted on the server, I get - An adapter cannot assign a new id to a record that already has an id.

The server has returned a new person record with id 2 and Ember errors trying to apply it to the model it has in it's store with id of 1.

So far I am overriding the updateId method of DS.Store:

export default DS.Store.extend({
  updateId: function(record, data) {
    var oldId = Ember.get(record, 'id');
    var id = coerceId(data.id);

    if (oldId && oldId !== id) {
      this.typeMapFor(record.constructor).idToRecord[oldId] = record;
    } else {
      this.typeMapFor(record.constructor).idToRecord[id] = record;
    }

    Ember.set(record, 'id', id);
  },
};

But this just feels wrong. Are there any negative consequences to this override? Or is there any other way to work around this?




Aucun commentaire:

Enregistrer un commentaire