Col.fetch() should create models with parse:true:

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.
This commit is contained in:
Magnus Holm
2011-12-04 18:13:00 +01:00
parent bdbcfa9da2
commit 67f689df9c
2 changed files with 21 additions and 2 deletions

View File

@@ -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;

View File

@@ -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() {