FIX: Don't remove the `id` from the input to `createRecord`

This commit is contained in:
Robin Ward 2016-07-06 13:52:30 -04:00
parent ee2780466c
commit 21684c98be
2 changed files with 17 additions and 2 deletions

View File

@ -268,7 +268,9 @@ export default Ember.Object.extend({
_hydrate(type, obj, root) {
if (!obj) { throw "Can't hydrate " + type + " of `null`"; }
if (!obj.id) { throw "Can't hydrate " + type + " without an `id`"; }
const id = obj.id;
if (!id) { throw "Can't hydrate " + type + " without an `id`"; }
root = root || obj;
@ -278,13 +280,14 @@ export default Ember.Object.extend({
this._hydrateEmbedded(type, obj, root);
}
const existing = fromMap(type, obj.id);
const existing = fromMap(type, id);
if (existing === obj) { return existing; }
if (existing) {
delete obj.id;
const klass = this.container.lookupFactory('model:' + type) || RestModel;
existing.setProperties(klass.munge(obj));
obj.id = id;
return existing;
}

View File

@ -19,6 +19,18 @@ test('createRecord without an `id`', function() {
ok(!widget.get('id'), 'there is no id');
});
test("createRecord doesn't modify the input `id` field", () => {
const store = createStore();
const widget = store.createRecord('widget', {id: 1, name: 'hello'});
const obj = { id: 1, name: 'something' };
const other = store.createRecord('widget', obj);
equal(widget, other, 'returns the same record');
equal(widget.name, 'something', 'it updates the properties');
equal(obj.id, 1, 'it does not remove the id from the input');
});
test('createRecord without attributes', function() {
const store = createStore();
const widget = store.createRecord('widget');