forked from emberjs/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Fix store.createRecord with belongsTo when model has @each o…
…bserver
- Loading branch information
1 parent
0999f8a
commit c257bbb
Showing
2 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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'); | ||
}); | ||
|
||
}); |