Created
June 20, 2017 17:22
-
-
Save arenoir/32be78c3201183617715d0ff655c7224 to your computer and use it in GitHub Desktop.
Address adapter unload existing record on create.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
); | |
} | |
); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
run.next
is not needed per this thread. You should userun.schedule('destroy', () => resolve(payload))
instead.