Skip to content

Commit

Permalink
Col.fetch() should create models with parse:true:
Browse files Browse the repository at this point in the history
This is done in two steps:

1. Col.fetch() now defaults to parse:true
2. Col.reset() and _prepareModel now passes along
   parse:true.

This means that Col.add() also accepts the
parse:true option.
  • Loading branch information
judofyr committed Dec 4, 2011
1 parent bdbcfa9 commit 67f689d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'); });
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 67f689d

Please sign in to comment.