emberjs/data — pull request #2510 by sebastianseilund (2014-11-25 19:15:24+00:00)
Title: A hasMany updated link should not remove new children

Hi there. I'm having an issue with a has-many link that gets loaded right as the parent record is created, which in turn removes all of the child records from the relationship in memory.

Example: `Team` has many `Player`s.

```js
App.Team = DS.Model.extend({
name: DS.attr('string'),
players: DS.hasMany('player', {async: true})
});

App.Player = DS.Model.extend({
name: DS.attr('string'),
team: DS.belongsTo('team')
});
```

The API expects creating a team to consist of `POST /teams` for the team, and then `POST /teams/:team_id/players` for each player. The response from `POST /teams` looks like this:

```js
{
team: {
id: '1',
name: 'Warriors',
links: {
players: '/teams/1/players'
}
}
}
```

Say I have created an unsaved team and a player for the team in the browser:

```js
var team = this.store.createRecord('team', {
name: 'Warriors'
})
this.store.createRecord('player', {
name: 'Steph Curry',
team: team
})
```

The player will show up correctly as belonging to the team.

But, after calling `team.save()`, the following will happen:

- `POST /teams`
- Ember Data gets back the above response body. It will update the `link` property for the team's `players` relationship, which calls `notifyPropertyChange` on the record for the `players` property.
- Since the `players` property is observed in the template, it's computed property will be recalculated right away.
- Ember Data will now see that there is a link, and will then load it using `GET /teams/1/players`.
- Since no players have been saved for that team yet, the server will respond `{players: []}`.
- Ember Data now updates the has-many relationship to be empty, and `Steph Curry` is gone.

I made a jsbin that shows the issue: http://emberjs.jsbin.com/rutixezeqe/1/edit?html,js,output. Click the Create button and notice how the players disappear until they are saved themselves.

I think that a possible solution would be that `ManyRelationship`'s '`computeChanges` did not remove records where `isNew=true`. The adapter would not know about these records, so it shouldn't be allowed to remove them.

I made a PR that fixes the issue and a test illustrating the scenario.

Any thoughts?