[Pkg-javascript-commits] [backbone] 31/37: #917, adding an Model#isValid() function.

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


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

js pushed a commit to tag 0.9.1
in repository backbone.

commit eb4753862e989e91d526ca4b012e6a891c22c7ad
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Thu Feb 2 15:54:34 2012 -0500

    #917, adding an Model#isValid() function.
---
 backbone.js   |  6 ++++++
 test/model.js | 12 ++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/backbone.js b/backbone.js
index d67ef4b..c4de3d7 100644
--- a/backbone.js
+++ b/backbone.js
@@ -435,6 +435,12 @@
       return _.clone(this._previousAttributes);
     },
 
+    // Check if the model is currently in a valid state. It's only possible to
+    // get into an *invalid* state if you're using silent changes.
+    isValid: function() {
+      return !this.validate(this.attributes);
+    },
+
     // Run validation against a set of incoming attributes, returning `true`
     // if all is well. If a specific `error` callback has been passed,
     // call that instead of firing the general `"error"` event.
diff --git a/test/model.js b/test/model.js
index 1e951b5..30f66e6 100644
--- a/test/model.js
+++ b/test/model.js
@@ -343,6 +343,18 @@ $(document).ready(function() {
     equal(lastError, "Can't change admin status.");
   });
 
+  test("Model: isValid", function() {
+    var model = new Backbone.Model({valid: true});
+    model.validate = function(attrs) {
+      if (!attrs.valid) return "invalid";
+    };
+    equal(model.isValid(), true);
+    equal(model.set({valid: false}), false);
+    equal(model.isValid(), true);
+    ok(model.set('valid', false, {silent: true}));
+    equal(model.isValid(), false);
+  });
+
   test("Model: save", function() {
     doc.save({title : "Henry V"});
     equal(lastRequest[0], 'update');

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