afeld / backbone-nested

A plugin to make Backbone.js keep track of nested attributes - looking for maintainers! https://github.com/afeld/backbone-nested/issues/157
https://afeld.github.com/backbone-nested/
MIT License
445 stars 83 forks source link

Using _.union to combine arrays alters arrays with non unique values #130

Open albertogasparin opened 9 years ago

albertogasparin commented 9 years ago

Imagine you have a default like myArray : []. Then, you initialize a model passing an initial value of myArray: [1,1,2,3]. The result of model.get('myArray') is not [1,1,2,3] but it is [1,2,3].

This issue is caused by this function:

combine = function(key) {
    return source[key] = _.union(destination[key], source[key]);
};

As a metter of fact, _.union returns all unique items inside the two arrays. So, if my destination[key] array is not unique the result will be wrong.