Skip to content

Commit

Permalink
Merge pull request #4931 from emberjs/fix-create-record
Browse files Browse the repository at this point in the history
[BUGFIX beta] [fixes #4509] createRecord initializes correctly
  • Loading branch information
stefanpenner authored Apr 18, 2017
2 parents 83c2e95 + f5e9d62 commit 9af5690
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
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);
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

0 comments on commit 9af5690

Please sign in to comment.