Skip to content

Commit

Permalink
merged.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 4, 2012
2 parents f918d9d + e5a3b0b commit a7be84e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 2 additions & 4 deletions backbone.js
Expand Up @@ -588,10 +588,7 @@
if (options.comparator !== void 0) this.comparator = options.comparator;
this._reset();
this.initialize.apply(this, arguments);
if (models) {
if (options.parse) models = this.parse(models);
this.reset(models, {silent: true, parse: options.parse});
}
if (models) this.reset(models, _.extend({silent: true}, options));
};

// Define the Collection's inheritable methods.
Expand Down Expand Up @@ -782,6 +779,7 @@
// you can reset the entire set with a new list of models, without firing
// any `add` or `remove` events. Fires `reset` when finished.
reset: function(models, options) {
if (options && options.parse) models = this.parse(models);
for (var i = 0, l = this.models.length; i < l; i++) {
this._removeReference(this.models[i]);
}
Expand Down
43 changes: 43 additions & 0 deletions test/collection.js
Expand Up @@ -726,4 +726,47 @@ $(document).ready(function() {
c.add({id: 4});
});

test("#1407 parse option on constructor parses collection and models", 2, function() {
var model = {
namespace : [{id: 1}, {id:2}]
};
var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({
parse: function(model) {
model.name = 'test';
return model;
}
}),
parse: function(model) {
return model.namespace;
}
});
var c = new Collection(model, {parse:true});

equal(c.length, 2);
equal(c.at(0).get('name'), 'test');
});

test("#1407 parse option on reset parses collection and models", 2, function() {
var model = {
namespace : [{id: 1}, {id:2}]
};
var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({
parse: function(model) {
model.name = 'test';
return model;
}
}),
parse: function(model) {
return model.namespace;
}
});
var c = new Collection();
c.reset(model, {parse:true});

equal(c.length, 2);
equal(c.at(0).get('name'), 'test');
});

});

0 comments on commit a7be84e

Please sign in to comment.