Skip to content

Commit

Permalink
Fixes #2262 -- always use parsed attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Mar 19, 2013
1 parent a0363e4 commit a920fed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,14 @@
// Turn bare objects into model references, and prevent invalid models
// from being added.
for (i = 0, l = models.length; i < l; i++) {
attrs = models[i];
if (!(model = this._prepareModel(attrs, options))) continue;
if (!(model = this._prepareModel(models[i], options))) continue;

// If a duplicate is found, prevent it from being added and
// optionally merge it into the existing model.
if (existing = this.get(model)) {
if (options.remove) modelMap[existing.cid] = true;
if (options.merge) {
existing.set(attrs === model ? model.attributes : attrs, options);
existing.set(model.attributes, options);
if (sortable && !sort && existing.hasChanged(sortAttr)) sort = true;
}
} else if (options.add) {
Expand Down
15 changes: 14 additions & 1 deletion test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ $(document).ready(function() {
equal(col.at(0).get('value'), 2);
});

test("add with parse and merge", function() {
var Model = Backbone.Model.extend({
parse: function (data) {
return data.model;
}
});
var collection = new Backbone.Collection();
collection.model = Model;
collection.add({id: 1});
collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true});
equal(collection.first().get('name'), 'Alf');
});

test("add model to collection with sort()-style comparator", 3, function() {
var col = new Backbone.Collection;
col.comparator = function(a, b) {
Expand Down Expand Up @@ -931,7 +944,7 @@ $(document).ready(function() {
equal(col.first().get('key'), 'other');

col.set({id: 1, other: 'value'});
equal(col.first().get('key'), 'other');
equal(col.first().get('key'), 'value');
equal(col.length, 1);
});

Expand Down

0 comments on commit a920fed

Please sign in to comment.