0
votes

When I try to create a model instance

record = this.store.createRecord('model', { /* whatever */ });
record.save();

And my API updates an already existing backend record instead of creating a new one. The API returns HTTP 200 [ok] (could also be 202 [accepted]) instead of 201 [created]. What is the ember way to have this record not created in the store if an instance of the same record is already there?

Right now if I "create" a record that turns out to update an existing record X times, I end up having the same record (with the same ID) repeated X times in my ember-data store.

1
I don't think I understand the question, why are you calling this.store.createRecord if what you want to do is update an existing record? - Asgaroth
I might not have this record loaded and do not wish to try and load it first, then send a create or update based on whether or not this record exists. Also, the rules for this record existing or being a new record are controlled by the API. - Jim

1 Answers

1
votes

When you use createRecord, you're telling Ember to add a new record to your store.

You need to fetch your record into your store first, if you want to update it:

this.store.find('model', id).then(function(record) {
  record.set('property', 'value');
  record.save();
});

http://emberjs.com/api/data/classes/DS.Store.html#method_createRecord

Maybe you're looking for this.store.update( ... ), depending on your specific needs: http://emberjs.com/api/data/classes/DS.Store.html#method_update