mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Forcing Collection.create to run through validation before saving.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user