Skip to content

Instantly share code, notes, and snippets.

@arenoir
Created June 20, 2017 17:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arenoir/32be78c3201183617715d0ff655c7224 to your computer and use it in GitHub Desktop.
Save arenoir/32be78c3201183617715d0ff655c7224 to your computer and use it in GitHub Desktop.
Address adapter unload existing record on create.
import Ember from 'ember';
import ApplicationAdapter from 'adapters/application';
const {RSVP, run} = Ember;
export default ApplicationAdapter.extend({
//unloads existing record if create record returns a duplcate.
createRecord(store, type, snapshot) {
let saving = this._super(store, type, snapshot);
return new RSVP.Promise(
(resolve, reject) => {
saving.then(
(payload) => {
let id = payload.address.id;
let existing = store.peekRecord('address', id);
if (existing) {
store.unloadRecord(existing);
run.next(() => { resolve(payload) });
} else {
resolve(payload);
}
},
(error) => {
reject(error)
}
);
}
);
},
});
@sukima
Copy link

sukima commented Apr 17, 2018

The run.next is not needed per this thread. You should use run.schedule('destroy', () => resolve(payload)) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment