diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Resource.js | 69 | ||||
| -rw-r--r-- | src/service/resource.js | 10 | ||||
| -rw-r--r-- | src/service/xhr.bulk.js | 60 | ||||
| -rw-r--r-- | src/service/xhr.cache.js | 90 | ||||
| -rw-r--r-- | src/service/xhr.js | 29 |
5 files changed, 171 insertions, 87 deletions
diff --git a/src/Resource.js b/src/Resource.js index 5462826d..3c149d8b 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -67,29 +67,36 @@ ResourceFactory.prototype = { forEach(actions, function(action, name){ var isPostOrPut = action.method == 'POST' || action.method == 'PUT'; - Resource[name] = function (a1, a2, a3) { + Resource[name] = function (a1, a2, a3, a4) { var params = {}; var data; - var callback = noop; + var success = noop; + var error = null; switch(arguments.length) { - case 3: callback = a3; + case 4: + error = a4; + success = a3; + case 3: case 2: if (isFunction(a2)) { - callback = a2; + success = a2; + error = a3; //fallthrough } else { params = a1; data = a2; + success = a3; break; } case 1: - if (isFunction(a1)) callback = a1; + if (isFunction(a1)) success = a1; else if (isPostOrPut) data = a1; else params = a1; break; case 0: break; default: - throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments."; + throw "Expected between 0-4 arguments [params, data, success, error], got " + + arguments.length + " arguments."; } var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data)); @@ -97,23 +104,20 @@ ResourceFactory.prototype = { action.method, route.url(extend({}, action.params || {}, extractParams(data), params)), data, - function(status, response, clear) { - if (200 <= status && status < 300) { - if (response) { - if (action.isArray) { - value.length = 0; - forEach(response, function(item){ - value.push(new Resource(item)); - }); - } else { - copy(response, value); - } + function(status, response) { + if (response) { + if (action.isArray) { + value.length = 0; + forEach(response, function(item) { + value.push(new Resource(item)); + }); + } else { + copy(response, value); } - (callback||noop)(value); - } else { - throw {status: status, response:response, message: status + ": " + response}; } + (success||noop)(value); }, + error || action.verifyCache, action.verifyCache); return value; }; @@ -122,18 +126,29 @@ ResourceFactory.prototype = { return self.route(url, extend({}, paramDefaults, additionalParamDefaults), actions); }; - Resource.prototype['$' + name] = function(a1, a2){ - var params = extractParams(this); - var callback = noop; + Resource.prototype['$' + name] = function(a1, a2, a3) { + var params = extractParams(this), + success = noop, + error; + switch(arguments.length) { - case 2: params = a1; callback = a2; - case 1: if (typeof a1 == $function) callback = a1; else params = a1; + case 3: params = a1; success = a2; error = a3; break; + case 2: + case 1: + if (isFunction(a1)) { + success = a1; + error = a2; + } else { + params = a1; + success = a2 || noop; + } case 0: break; default: - throw "Expected between 1-2 arguments [params, callback], got " + arguments.length + " arguments."; + throw "Expected between 1-3 arguments [params, success, error], got " + + arguments.length + " arguments."; } var data = isPostOrPut ? this : undefined; - Resource[name].call(this, params, data, callback); + Resource[name].call(this, params, data, success, error); }; }); return Resource; diff --git a/src/service/resource.js b/src/service/resource.js index 31d7ceeb..c11067b1 100644 --- a/src/service/resource.js +++ b/src/service/resource.js @@ -82,9 +82,9 @@ * The action methods on the class object or instance object can be invoked with the following * parameters: * - * - HTTP GET "class" actions: `Resource.action([parameters], [callback])` - * - non-GET "class" actions: `Resource.action(postData, [parameters], [callback])` - * - non-GET instance actions: `instance.$action([parameters], [callback])` + * - HTTP GET "class" actions: `Resource.action([parameters], [success], [error])` + * - non-GET "class" actions: `Resource.action(postData, [parameters], [success], [error])` + * - non-GET instance actions: `instance.$action([parameters], [success], [error])` * * * @example @@ -142,8 +142,8 @@ }); </pre> * - * It's worth noting that the callback for `get`, `query` and other method gets passed in the - * response that came from the server, so one could rewrite the above example as: + * It's worth noting that the success callback for `get`, `query` and other method gets passed + * in the response that came from the server, so one could rewrite the above example as: * <pre> var User = $resource('/user/:userId', {userId:'@id'}); diff --git a/src/service/xhr.bulk.js b/src/service/xhr.bulk.js index c0940d9d..d7fc7990 100644 --- a/src/service/xhr.bulk.js +++ b/src/service/xhr.bulk.js @@ -15,9 +15,10 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){ var requests = [], scope = this; - function bulkXHR(method, url, post, callback) { + function bulkXHR(method, url, post, success, error) { if (isFunction(post)) { - callback = post; + error = success; + success = post; post = null; } var currentQueue; @@ -28,32 +29,55 @@ angularServiceInject('$xhr.bulk', function($xhr, $error, $log){ }); if (currentQueue) { if (!currentQueue.requests) currentQueue.requests = []; - currentQueue.requests.push({method: method, url: url, data:post, callback:callback}); + var request = { + method: method, + url: url, + data: post, + success: success}; + if (error) request.error = error; + currentQueue.requests.push(request); } else { - $xhr(method, url, post, callback); + $xhr(method, url, post, success, error); } } bulkXHR.urls = {}; - bulkXHR.flush = function(callback){ - forEach(bulkXHR.urls, function(queue, url){ + bulkXHR.flush = function(success, error) { + forEach(bulkXHR.urls, function(queue, url) { var currentRequests = queue.requests; if (currentRequests && currentRequests.length) { queue.requests = []; queue.callbacks = []; - $xhr('POST', url, {requests:currentRequests}, function(code, response){ - forEach(response, function(response, i){ - try { - if (response.status == 200) { - (currentRequests[i].callback || noop)(response.status, response.response); - } else { - $error(currentRequests[i], response); + $xhr('POST', url, {requests: currentRequests}, + function(code, response) { + forEach(response, function(response, i) { + try { + if (response.status == 200) { + (currentRequests[i].success || noop)(response.status, response.response); + } else if (isFunction(currentRequests[i].error)) { + currentRequests[i].error(response.status, response.response); + } else { + $error(currentRequests[i], response); + } + } catch(e) { + $log.error(e); } - } catch(e) { - $log.error(e); - } + }); + (success || noop)(); + }, + function(code, response) { + forEach(currentRequests, function(request, i) { + try { + if (isFunction(request.error)) { + request.error(code, response); + } else { + $error(request, response); + } + } catch(e) { + $log.error(e); + } + }); + (error || noop)(); }); - (callback || noop)(); - }); scope.$eval(); } }); diff --git a/src/service/xhr.cache.js b/src/service/xhr.cache.js index 42b666e1..256b936e 100644 --- a/src/service/xhr.cache.js +++ b/src/service/xhr.cache.js @@ -5,7 +5,11 @@ * @ngdoc service * @name angular.service.$xhr.cache * @function - * @requires $xhr + * + * @requires $xhr.bulk + * @requires $defer + * @requires $xhr.error + * @requires $log * * @description * Acts just like the {@link angular.service.$xhr $xhr} service but caches responses for `GET` @@ -18,27 +22,42 @@ * @param {string} method HTTP method. * @param {string} url Destination URL. * @param {(string|Object)=} post Request body. - * @param {function(number, (string|Object))} callback Response callback. + * @param {function(number, (string|Object))} success Response success callback. + * @param {function(number, (string|Object))=} error Response error callback. * @param {boolean=} [verifyCache=false] If `true` then a result is immediately returned from cache * (if present) while a request is sent to the server for a fresh response that will update the - * cached entry. The `callback` function will be called when the response is received. - * @param {boolean=} [sync=false] in case of cache hit execute `callback` synchronously. + * cached entry. The `success` function will be called when the response is received. + * @param {boolean=} [sync=false] in case of cache hit execute `success` synchronously. */ -angularServiceInject('$xhr.cache', function($xhr, $defer, $log){ +angularServiceInject('$xhr.cache', function($xhr, $defer, $error, $log) { var inflight = {}, self = this; - function cache(method, url, post, callback, verifyCache, sync){ + function cache(method, url, post, success, error, verifyCache, sync) { if (isFunction(post)) { - callback = post; + if (!isFunction(success)) { + verifyCache = success; + sync = error; + error = null; + } else { + sync = verifyCache; + verifyCache = error; + error = success; + } + success = post; post = null; + } else if (!isFunction(error)) { + sync = verifyCache; + verifyCache = error; + error = null; } + if (method == 'GET') { var data, dataCached; if (dataCached = cache.data[url]) { if (sync) { - callback(200, copy(dataCached.value)); + success(200, copy(dataCached.value)); } else { - $defer(function() { callback(200, copy(dataCached.value)); }); + $defer(function() { success(200, copy(dataCached.value)); }); } if (!verifyCache) @@ -46,30 +65,51 @@ angularServiceInject('$xhr.cache', function($xhr, $defer, $log){ } if (data = inflight[url]) { - data.callbacks.push(callback); + data.successes.push(success); + data.errors.push(error); } else { - inflight[url] = {callbacks: [callback]}; - cache.delegate(method, url, post, function(status, response){ - if (status == 200) - cache.data[url] = { value: response }; - var callbacks = inflight[url].callbacks; - delete inflight[url]; - forEach(callbacks, function(callback){ - try { - (callback||noop)(status, copy(response)); - } catch(e) { - $log.error(e); - } + inflight[url] = {successes: [success], errors: [error]}; + cache.delegate(method, url, post, + function(status, response) { + if (status == 200) + cache.data[url] = {value: response}; + var successes = inflight[url].successes; + delete inflight[url]; + forEach(successes, function(success) { + try { + (success||noop)(status, copy(response)); + } catch(e) { + $log.error(e); + } + }); + }, + function(status, response) { + var errors = inflight[url].errors, + successes = inflight[url].successes; + delete inflight[url]; + + forEach(errors, function(error, i) { + try { + if (isFunction(error)) { + error(status, copy(response)); + } else { + $error( + {method: method, url: url, data: post, success: successes[i]}, + {status: status, body: response}); + } + } catch(e) { + $log.error(e); + } + }); }); - }); } } else { cache.data = {}; - cache.delegate(method, url, post, callback); + cache.delegate(method, url, post, success, error); } } cache.data = {}; cache.delegate = $xhr; return cache; -}, ['$xhr.bulk', '$defer', '$log']); +}, ['$xhr.bulk', '$defer', '$xhr.error', '$log']); diff --git a/src/service/xhr.js b/src/service/xhr.js index 5fc5223e..dc18419d 100644 --- a/src/service/xhr.js +++ b/src/service/xhr.js @@ -21,10 +21,10 @@ * {@link angular.service.$resource $resource} service. * * # Error handling - * All XHR responses with response codes other then `2xx` are delegated to - * {@link angular.service.$xhr.error $xhr.error}. The `$xhr.error` can intercept the request - * and process it in application specific way, or resume normal execution by calling the - * request callback method. + * If no `error callback` is specified, XHR response with response code other then `2xx` will be + * delegated to {@link angular.service.$xhr.error $xhr.error}. The `$xhr.error` can intercept the + * request and process it in application specific way, or resume normal execution by calling the + * request `success` method. * * # HTTP Headers * The $xhr service will automatically add certain http headers to all requests. These defaults can @@ -96,14 +96,16 @@ * angular generated callback function. * @param {(string|Object)=} post Request content as either a string or an object to be stringified * as JSON before sent to the server. - * @param {function(number, (string|Object))} callback A function to be called when the response is - * received. The callback will be called with: + * @param {function(number, (string|Object))} success A function to be called when the response is + * received. The success function will be called with: * * - {number} code [HTTP status code](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes) of * the response. This will currently always be 200, since all non-200 responses are routed to - * {@link angular.service.$xhr.error} service. + * {@link angular.service.$xhr.error} service (or custom error callback). * - {string|Object} response Response object as string or an Object if the response was in JSON * format. + * @param {function(number, (string|Object))} error A function to be called if the response code is + * not 2xx.. Accepts the same arguments as success, above. * * @example <doc:example> @@ -158,9 +160,10 @@ angularServiceInject('$xhr', function($browser, $error, $log, $updateView){ patch: {} }; - function xhr(method, url, post, callback){ + function xhr(method, url, post, success, error) { if (isFunction(post)) { - callback = post; + error = success; + success = post; post = null; } if (post && isObject(post)) { @@ -176,11 +179,13 @@ angularServiceInject('$xhr', function($browser, $error, $log, $updateView){ } } if (200 <= code && code < 300) { - callback(code, response); + success(code, response); + } else if (isFunction(error)) { + error(code, response); } else { $error( - {method: method, url:url, data:post, callback:callback}, - {status: code, body:response}); + {method: method, url: url, data: post, success: success}, + {status: code, body: response}); } } catch (e) { $log.error(e); |
