Permalink
Browse files

createModel is now able to ignore bad data by returning a falsy value.

  • Loading branch information...
epeli committed Apr 9, 2012
1 parent 31fdd97 commit 58952deb6d3c2cd88f7c76104ce55da183d0e52f
Showing with 14 additions and 1 deletion.
  1. +1 −1 backbone.js
  2. +13 −0 test/collection.js
View
@@ -821,7 +821,7 @@
var attrs = model;
options.collection = this;
model = this.createModel(attrs, options);
- if (!model._validate(model.attributes, options)) model = false;
+ if (!model || !model._validate(model.attributes, options)) model = false;
} else if (!model.collection) {
model.collection = this;
}
View
@@ -594,4 +594,17 @@ $(document).ready(function() {
equal(c.at(1).customAttr, "b");
});
+ test("Collection: createModel can ignore data by returning a falsy value", function() {
+ var Col = Backbone.Collection.extend({
+ createModel: function(attrs, options) {
+ if (attrs.error) return;
+ return new this.model(attrs, options);
+ }
+ });
+ var c = new Col();
+ c.create({ error: "Bad model data" });
+ c.create({ foo: "bar" });
+ equal(c.length, 1);
+ });
+
});

0 comments on commit 58952de

Please sign in to comment.