Skip to content

Commit

Permalink
Merge pull request jashkenas#2477 from caseywebdev/merge-defaults
Browse files Browse the repository at this point in the history
Fix jashkenas#2471 merging with defaults
  • Loading branch information
jashkenas committed Apr 18, 2013
2 parents f61bd29 + 8141f4f commit c3a1af0
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
options._attrs = attrs;
if (defaults = _.result(this, 'defaults')) {
attrs = _.defaults({}, attrs, defaults);
}
Expand Down Expand Up @@ -675,14 +676,15 @@
// Turn bare objects into model references, and prevent invalid models
// from being added.
for (i = 0, l = models.length; i < l; i++) {
if (!(model = this._prepareModel(models[i], options))) continue;
if (!(model = this._prepareModel(attrs = 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 (remove) modelMap[existing.cid] = true;
if (merge) {
existing.set(model.attributes, options);
attrs = attrs === model ? model.attributes : options._attrs;
existing.set(attrs, options);
if (sortable && !sort && existing.hasChanged(sortAttr)) sort = true;
}

Expand Down

0 comments on commit c3a1af0

Please sign in to comment.