Skip to content

Commit

Permalink
Merge pull request #5369 from dwickern/fix-create-duplicate
Browse files Browse the repository at this point in the history
[BUGFIX beta] Fix createRecord creating two records
  • Loading branch information
mmun authored Mar 4, 2018
2 parents 1c09f9f + d51e1a2 commit af9a18d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
6 changes: 1 addition & 5 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default class InternalModel {
return this.currentState.dirtyType;
}

getRecord(properties) {
getRecord() {
if (!this._record && !this._isDematerializing) {
heimdall.increment(materializeRecord);
let token = heimdall.start('InternalModel.getRecord');
Expand All @@ -350,10 +350,6 @@ export default class InternalModel {
adapterError: this.error
};

if (typeof properties === 'object' && properties !== null) {
emberAssign(createOptions, properties);
}

if (setOwner) {
// ensure that `getOwner(this)` works inside a model instance
setOwner(createOptions, getOwner(this.store));
Expand Down
3 changes: 2 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ Store = Service.extend({

let internalModel = this._buildInternalModel(normalizedModelName, properties.id);
internalModel.loadedData();
let record = internalModel.getRecord(properties);
let record = internalModel.getRecord();
record.setProperties(properties);

// TODO @runspired this should also be coalesced into some form of internalModel.setState()
internalModel.eachRelationship((key, descriptor) => {
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/relationships/one-to-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1452,3 +1452,35 @@ test("Rollbacking attributes of a created record works correctly when the belong
assert.equal(user.get('accounts.length'), 0, "User does not have the account anymore");
assert.equal(account.get('user'), null, 'Account does not have the user anymore');
});

test("createRecord updates inverse record array which has observers", function(assert) {

env.adapter.findAll = () => {
return {
data: [{
id: '2',
type: 'user',
attributes: {
name: 'Stanley'
}
}]
}
};

return store.findAll('user').then(users => {
assert.equal(users.get('length'), 1, 'Exactly 1 user');

let user = users.get('firstObject');
assert.equal(user.get('messages.length'), 0, 'Record array is initially empty');

// set up an observer
user.addObserver('[email protected]', () => {});
user.get('messages.firstObject');

let message = run(() => store.createRecord('message', { user, title: 'EmberFest was great' }));
assert.equal(user.get('messages.length'), 1, 'The message is added to the record array');

let messageFromArray = user.get('messages.firstObject');
assert.equal(message, messageFromArray, 'Only one message should be created');
});
});

0 comments on commit af9a18d

Please sign in to comment.