Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: push leftHandSide relationship info for findHasMany/belongsTo
always pushes the left hand side of a relationship, even when the adapter does not return a payload with the left hand side's ids in `relationships`. What a mouthful. Let's unpack that. Given the following model definitions: ```javascript // app/models/person.js export default DS.Model.extend({ name: attr(), dogs: hasMany('dog', { async: true }) }); ``` ```javascript // app/models/dog.js export default DS.Model.extend({ name: attr(), person: belongsTo('person', { async: true }) }); ``` Given `PersonAdapter#findRecord` returns the following payload and is pushed into the store: ```javascript const payload = { data: { type: 'person', id: '1', attributes: { name: 'John Churchill' }, relationships: { dogs: { links: { related: 'http://exmaple.com/person/1/dogs' } } } } }; ``` and the result of `DogAdapter#findHasMany` resolves with the following payload: ```javascript { data: [ { id: 1, type: 'dog', attributes: { name: 'Scooby' } }, { id: 2, type: 'dog', attributes: { name: 'Scrappy' } } ] } ``` Notice how the payload for `DogAdapter#findHasMany` does not have a `relationships` key for any of the returned dogs. This is technically valid JSONAPI, but in today's Ember Data, *for explicit inverses only* (notice how we specified 'person' on the Dog model; an implicit relationship would be generated for us if we left it out and used internally for tracking various things), the inverse relationship does not get set up. For example, `dog.get('person.id')` would still be null when it should be `1` (the `id` of the Person it belongsTo). This commit fixes that by always pushing the inverse relationship using the store's RelationshipPayloadsManager. In this example, the following relationship info is pushed: ```javascript { data: { id: '1', type: 'person' } } ``` This makes the result of requesting `DogAdapter#findHasMany` effectively the following payload: ```javascript { data: [ { id: 1, type: 'dog', attributes: { name: 'Scooby' }, relationships: { id: '1', type: 'person' } }, { id: 2, type: 'dog', attributes: { name: 'Scrappy' }, relationships: { id: '1', type: 'person' } } ] } ``` Adding this information makes the relationships line up correctly.
- Loading branch information