Skip to content

Commit

Permalink
[BUGFIX beta] fixes #4149, new record does not added twice in many
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Chatry committed Feb 12, 2016
1 parent 567dfc5 commit d9c62c4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {

//a hack for not removing new records
//TODO remove once we have proper diffing
var newRecords = this.currentState.filter((internalModel) => internalModel.isNew());
var newRecords = this.currentState.filter(
(internalModel) => internalModel.isNew() && toSet.indexOf(internalModel) === -1
);
toSet = toSet.concat(newRecords);
var oldLength = this.length;
this.arrayContentWillChange(0, this.length, toSet.length);
Expand Down
58 changes: 58 additions & 0 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,64 @@ test("A hasMany updated link should not remove new children when the parent reco
});
});

test("A hasMany updated link should not remove new children when the parent record has children already with feedback from the server after save", function(assert) {

Post.reopen({
comments: DS.hasMany('comment', { async: true })
});

Comment.reopen({
message: DS.belongsTo('post', { async: true })
});

env.adapter.createRecord = function(store, snapshot, link, relationship) {
return Ember.RSVP.resolve({
id: 5
});
};


env.adapter.findHasMany = function(store, snapshot, link, relationship) {
return Ember.RSVP.resolve([{ id: 5 }]);
};


run(function() {
var post = env.store.createRecord('post', {});
env.store.createRecord('comment', { id: 5, message: post });

post.get('comments')
.then(function(comments) {
assert.equal(comments.get('length'), 1);
return post.save();
})
.then(function() {
return env.store.push({
data: {
type: "post",
id: 5,
relationships: {
comments: {
data: [
{
id: 5,
type: "comment"
}
]
}
}
}
});
})
.then(function() {
return post.get('comments');
})
.then(function(comments) {
assert.equal(comments.get('length'), 1);
});
});
});


test("A hasMany relationship can be reloaded if it was fetched via a link", function(assert) {
Post.reopen({
Expand Down

0 comments on commit d9c62c4

Please sign in to comment.