diff --git a/backbone.js b/backbone.js index 805ce42c..ea449c56 100644 --- a/backbone.js +++ b/backbone.js @@ -475,7 +475,7 @@ options || (options = {}); this.each(this._removeReference); this._reset(); - this.add(models, {silent: true}); + this.add(models, {silent: true, parse: options.parse}); if (!options.silent) this.trigger('reset', this, options); return this; }, @@ -485,6 +485,7 @@ // models to the collection instead of resetting. fetch : function(options) { options || (options = {}); + if (options.parse === undefined) options.parse = true; var collection = this; var success = options.success; options.success = function(resp, status, xhr) { @@ -537,7 +538,7 @@ _prepareModel : function(model, options) { if (!(model instanceof Backbone.Model)) { var attrs = model; - model = new this.model(attrs, {collection: this}); + model = new this.model(attrs, {collection: this, parse: options.parse}); if (model.validate && !model._performValidation(model.attributes, options)) model = false; } else if (!model.collection) { model.collection = this; diff --git a/test/collection.js b/test/collection.js index 80beeb98..005b0447 100644 --- a/test/collection.js +++ b/test/collection.js @@ -179,6 +179,20 @@ $(document).ready(function() { equals(e.collection, colE); }); + test("Collection: add model with parse", function() { + var Model = Backbone.Model.extend({ + parse: function(obj) { + obj.value += 1; + return obj; + } + }); + + var Col = Backbone.Collection.extend({model: Model}); + var col = new Col; + col.add({value: 1}, {parse: true}); + equals(col.at(0).get('value'), 2); + }); + test("Collection: remove", function() { var removed = otherRemoved = null; col.bind('remove', function(model){ removed = model.get('label'); }); @@ -304,6 +318,10 @@ $(document).ready(function() { col.fetch(); equals(lastRequest[0], 'read'); equals(lastRequest[1], col); + equals(lastRequest[2].parse, true); + + col.fetch({parse: false}); + equals(lastRequest[2].parse, false); }); test("Collection: create", function() {