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] [fixes #4509] createRecord initializes correctly #4931

Merged
merged 1 commit into from
Apr 18, 2017
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: 5 additions & 1 deletion addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export default class InternalModel {
return this.currentState.dirtyType;
}

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

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

if (setOwner) {
// ensure that `getOwner(this)` works inside a model instance
setOwner(createOptions, getOwner(this.store));
Expand Down
10 changes: 1 addition & 9 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,8 @@ Store = Service.extend({
properties.id = coerceId(properties.id);

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

// Move the record out of its initial `empty` state into
// the `loaded` state.
// TODO @runspired this seems really bad, store should not be changing the state
internalModel.loadedData();

// Set the properties specified on the record.
// TODO @runspired this is probably why we do the bad thing above
record.setProperties(properties);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay! I like to see my TODOs removed validly :D

let record = internalModel.getRecord(properties);

// TODO @runspired this should also be coalesced into some form of internalModel.setState()
internalModel.eachRelationship((key, descriptor) => {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,27 @@ test('toJSON looks up the JSONSerializer using the store instead of using JSONSe
assert.deepEqual(json, {});
});

test('internalModel is ready by `init`', function(assert) {
assert.expect(2);
let nameDidChange = 0;

const Person = DS.Model.extend({
name: DS.attr('string'),

init() {
this._super(...arguments);
this.set('name', 'my-name-set-in-init');
},

nameDidChange: Ember.observer('name', () => nameDidChange++)
});

let { store } = setupStore({ person: Person });

assert.equal(nameDidChange, 0, 'observer should not trigger on create');
let person = run(() => store.createRecord('person'));
assert.equal(person.get('name'), 'my-name-set-in-init');
});

test('accessing attributes in the initializer should not throw an error', function(assert) {
assert.expect(1);
Expand Down