Forcing Collection.create to run through validation before saving.

This commit is contained in:
Kris Jordan
2011-02-06 01:13:52 -05:00
parent 79b33ae78e
commit 7442f826f1
2 changed files with 16 additions and 1 deletions

View File

@@ -511,7 +511,9 @@
var coll = this;
options || (options = {});
if (!(model instanceof Backbone.Model)) {
model = new this.model(model, {collection: coll});
modelRef = model;
model = new this.model(null, {collection: coll});
if(!model.set(modelRef)) return false;
} else {
model.collection = coll;
}

View File

@@ -208,6 +208,19 @@ $(document).ready(function() {
equals(model.collection, col);
});
test("Collection: create enforces validation", function() {
var ValidatingModel = Backbone.Model.extend({
validate: function(attrs) {
return "fail"
}
});
var ValidatingCollection = Backbone.Collection.extend({
model: ValidatingModel
});
var col = new ValidatingCollection();
equals(col.create({"foo":"bar"}),false);
});
test("collection: initialize", function() {
var Collection = Backbone.Collection.extend({
initialize: function() {