Skip to content

Commit

Permalink
Merge pull request jashkenas#3032 from akre54/clone-opts
Browse files Browse the repository at this point in the history
Pass along model and comparator to cloned collection
  • Loading branch information
jashkenas committed Mar 3, 2014
2 parents bdc5c63 + 16e5483 commit 0c1838b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,10 @@

// Create a new collection with an identical list of models as this one.
clone: function() {
return new this.constructor(this.models);
return new this.constructor(this.models, {
model: this.model,
comparator: this.comparator
});
},

// Private method to reset all internal state. Called when the collection
Expand Down
11 changes: 11 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
strictEqual(collection.last().get('a'), 4);
});

test("clone preserves model and comparator", 3, function() {
var Model = Backbone.Model.extend(),
comparator = function() {};

var col = (new Backbone.Collection([{id: 1}], {model: Model, comparator: comparator})).clone();
col.add({id: 2});
ok(col.at(0) instanceof Model);
ok(col.at(1) instanceof Model);
strictEqual(col.comparator, comparator);
});

test("get", 6, function() {
equal(col.get(0), d);
equal(col.get(d.clone()), d);
Expand Down

0 comments on commit 0c1838b

Please sign in to comment.