Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Fix createRecord creating two records #5369

Merged
merged 3 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});