[Pkg-javascript-commits] [backbone] 61/211: Forcing Collection.create to run through validation before saving.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.5.0
in repository backbone.

commit 7442f826f1b66364071705378a79480e9ef6a03e
Author: Kris Jordan <krisjordan at gmail.com>
Date:   Sun Feb 6 01:13:52 2011 -0500

    Forcing Collection.create to run through validation before saving.
---
 backbone.js        |  4 +++-
 test/collection.js | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index e323ed0..60747da 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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;
       }
diff --git a/test/collection.js b/test/collection.js
index 9c23cfd..ba93619 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -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() {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list