[Pkg-javascript-commits] [backbone] 04/12: first draft of REST-failure 'error' events.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:40 UTC 2014


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

js pushed a commit to tag 0.3.3
in repository backbone.

commit 160f0ba9dabefc093b67e83ebb380f18dd55a367
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Mon Nov 29 13:33:07 2010 -0500

    first draft of REST-failure 'error' events.
---
 backbone.js | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/backbone.js b/backbone.js
index a8fbefe..4ca830a 100644
--- a/backbone.js
+++ b/backbone.js
@@ -245,7 +245,7 @@
         if (!model.set(model.parse(resp), options)) return false;
         if (options.success) options.success(model, resp);
       };
-      var error = options.error && _.bind(options.error, null, model);
+      var error = wrapError(options.error, model);
       (this.sync || Backbone.sync)('read', this, success, error);
       return this;
     },
@@ -261,7 +261,7 @@
         if (!model.set(model.parse(resp), options)) return false;
         if (options.success) options.success(model, resp);
       };
-      var error = options.error && _.bind(options.error, null, model);
+      var error = wrapError(options.error, model);
       var method = this.isNew() ? 'create' : 'update';
       (this.sync || Backbone.sync)(method, this, success, error);
       return this;
@@ -276,7 +276,7 @@
         if (model.collection) model.collection.remove(model);
         if (options.success) options.success(model, resp);
       };
-      var error = options.error && _.bind(options.error, null, model);
+      var error = wrapError(options.error, model);
       (this.sync || Backbone.sync)('delete', this, success, error);
       return this;
     },
@@ -483,7 +483,7 @@
         collection.refresh(collection.parse(resp));
         if (options.success) options.success(collection, resp);
       };
-      var error = options.error && _.bind(options.error, null, collection);
+      var error = wrapError(options.error, collection);
       (this.sync || Backbone.sync)('read', this, success, error);
       return this;
     },
@@ -993,6 +993,17 @@
     return _.isFunction(object.url) ? object.url() : object.url;
   };
 
+  // Wrap an optional error callback with a fallback error event.
+  var wrapError = function(onError, model) {
+    return function(resp) {
+      if (onError) {
+        onError(model, resp);
+      } else {
+        model.trigger('error', model, resp);
+      }
+    };
+  };
+
   // Helper function to escape a string for HTML rendering.
   var escapeHTML = function(string) {
     return string.replace(/&(?!\w+;)/g, '&')

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