Skip to content

Commit

Permalink
[BUGFIX] Fix store.createRecord with belongsTo when model has @each o…
Browse files Browse the repository at this point in the history
…bserver
  • Loading branch information
mattraydub committed Dec 13, 2017
1 parent 0999f8a commit c257bbb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
5 changes: 1 addition & 4 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,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 All @@ -350,6 +346,7 @@ export default class InternalModel {
}

this._record = this.store.modelFactoryFor(this.modelName).create(createOptions);
this._record.setProperties(properties);

this._triggerDeferredTriggers();
heimdall.stop(token);
Expand Down
63 changes: 62 additions & 1 deletion tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get } from '@ember/object';
import { get, computed } from '@ember/object';
import { run } from '@ember/runloop';
import RSVP, { resolve } from 'rsvp';
import setupStore from 'dummy/tests/helpers/store';
Expand Down Expand Up @@ -1541,3 +1541,64 @@ test("belongsTo relationship with links doesn't trigger extra change notificatio

assert.equal(count, 0);
});

function tap(obj, methodName, callback) {
var old = obj[methodName];

var summary = { called: [] };

obj[methodName] = function() {
var result = old.apply(obj, arguments);
if (callback) {
callback.apply(obj, arguments);
}
summary.called.push(arguments);
return result;
};

return summary;
}

test('passing belongsTo relationship during create (on Model with property using @each observer) does not create extra records', function(assert) {
const Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person', { inverse: 'tag' }),

// special property that uses @each
peopleNames: computed('[email protected]', function() {
return get(this, 'people').mapBy('name');
})
});

const Person = DS.Model.extend({
name: DS.attr('string'),
tag: DS.belongsTo('tag', { inverse: 'people' })
});

let env = setupStore({ tag: Tag, person: Person });
let { store } = env;
let personCreate = tap(Person, 'create');

run(() => {
store.push({
data: [{
type: 'tag',
id: 1,
attributes: {
name: 'whatever'
}
}]
});

const tag1 = store.recordForId('tag', 1);

// access the property with the @each observer
get(tag1, 'peopleNames');

// create the new person
store.createRecord('person', { name: 'newPerson', tag: tag1 });

assert.equal(personCreate.called.length, 1, 'personCreate should be called 1 time for new Person');
});

});

0 comments on commit c257bbb

Please sign in to comment.