[Pkg-javascript-commits] [backbone] 30/34: error events are now always passed the model as the first argument. You may now also pass an error callback to set() and save(), if the callback is passed, it will be called instead of the 'error' event getting fired.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:46 UTC 2014


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

js pushed a commit to tag 0.1.2
in repository backbone.

commit a09bcbca9d660b09dd010e6fc92c5c6c32f10bbb
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Tue Oct 19 10:13:50 2010 -0400

    error events are now always passed the model as the first argument. You may now also pass an error callback to set() and save(), if the callback is passed, it will be called instead of the 'error' event getting fired.
---
 backbone.js   | 26 +++++++++++++++++---------
 test/model.js | 24 ++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/backbone.js b/backbone.js
index 272a7e7..501224b 100644
--- a/backbone.js
+++ b/backbone.js
@@ -142,11 +142,16 @@
       if (attrs.attributes) attrs = attrs.attributes;
       var now = this.attributes;
 
-      // Run validation if `validate` is defined.
+      // Run validation if `validate` is defined. If a specific `error` callback
+      // has been passed, call that instead of firing the general `"error"` event.
       if (this.validate) {
         var error = this.validate(attrs);
         if (error) {
-          this.trigger('error', this, error);
+          if (options.error) {
+            options.error(this, error);
+          } else {
+            this.trigger('error', this, error);
+          }
           return false;
         }
       }
@@ -193,10 +198,11 @@
       options || (options = {});
       var model = this;
       var success = function(resp) {
-        if (!model.set(resp.model)) return false;
+        if (!model.set(resp.model, options)) return false;
         if (options.success) options.success(model, resp);
       };
-      Backbone.sync('read', this, success, options.error);
+      var error = options.error && _.bind(options.error, null, model);
+      Backbone.sync('read', this, success, error);
       return this;
     },
 
@@ -209,11 +215,12 @@
       if (!this.set(attrs, options)) return false;
       var model = this;
       var success = function(resp) {
-        if (!model.set(resp.model)) return false;
+        if (!model.set(resp.model, options)) return false;
         if (options.success) options.success(model, resp);
       };
+      var error = options.error && _.bind(options.error, null, model);
       var method = this.isNew() ? 'create' : 'update';
-      Backbone.sync(method, this, success, options.error);
+      Backbone.sync(method, this, success, error);
       return this;
     },
 
@@ -226,7 +233,8 @@
         if (model.collection) model.collection.remove(model);
         if (options.success) options.success(model, resp);
       };
-      Backbone.sync('delete', this, success, options.error);
+      var error = options.error && _.bind(options.error, null, model);
+      Backbone.sync('delete', this, success, error);
       return this;
     },
 
@@ -399,7 +407,8 @@
         collection.refresh(resp.models);
         if (options.success) options.success(collection, resp);
       };
-      Backbone.sync('read', this, success, options.error);
+      var error = options.error && _.bind(options.error, null, collection);
+      Backbone.sync('read', this, success, error);
       return this;
     },
 
@@ -410,7 +419,6 @@
       if (!(model instanceof Backbone.Model)) model = new this.model(model);
       model.collection = this;
       var success = function(resp) {
-        if (!model.set(resp.model)) return false;
         model.collection.add(model);
         if (options.success) options.success(model, resp);
       };
diff --git a/test/model.js b/test/model.js
index 82a5345..1573bb0 100644
--- a/test/model.js
+++ b/test/model.js
@@ -138,4 +138,28 @@ $(document).ready(function() {
     equals(lastError, "Can't change admin status.");
   });
 
+  test("Model: validate with error callback", function() {
+    var lastError, boundError;
+    var model = new Backbone.Model();
+    model.validate = function(attrs) {
+      if (attrs.admin) return "Can't change admin status.";
+    };
+    var callback = function(model, error) {
+      lastError = error;
+    };
+    model.bind('error', function(model, error) {
+      boundError = true;
+    });
+    var result = model.set({a: 100}, {error: callback});
+    equals(result, model);
+    equals(model.get('a'), 100);
+    equals(lastError, undefined);
+    equals(boundError, undefined);
+    result = model.set({a: 200, admin: true}, {error: callback});
+    equals(result, false);
+    equals(model.get('a'), 100);
+    equals(lastError, "Can't change admin status.");
+    equals(boundError, undefined);
+  });
+
 });

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