refactor for simplicity

This commit is contained in:
Misko Hevery 2010-05-19 11:54:58 -07:00
parent 0f73084e9d
commit f2abbfd394

View file

@ -231,9 +231,8 @@ angularService('$xhr.error', function($log){
}; };
}, {inject:['$log']}); }, {inject:['$log']});
angularService('$xhr.bulk', function($xhr, $error){ angularService('$xhr.bulk', function($xhr, $error, $log){
var requests = [], var requests = [],
callbacks = [],
scope = this; scope = this;
function bulkXHR(method, url, post, callback) { function bulkXHR(method, url, post, callback) {
if (isFunction(post)) { if (isFunction(post)) {
@ -248,9 +247,7 @@ angularService('$xhr.bulk', function($xhr, $error){
}); });
if (currentQueue) { if (currentQueue) {
if (!currentQueue.requests) currentQueue.requests = []; if (!currentQueue.requests) currentQueue.requests = [];
if (!currentQueue.callbacks) currentQueue.callbacks = []; currentQueue.requests.push({method: method, url: url, data:post, callback:callback});
currentQueue.requests.push({method: method, url: url, data:post});
currentQueue.callbacks.push(callback);
} else { } else {
$xhr(method, url, post, callback); $xhr(method, url, post, callback);
} }
@ -258,8 +255,7 @@ angularService('$xhr.bulk', function($xhr, $error){
bulkXHR.urls = {}; bulkXHR.urls = {};
bulkXHR.flush = function(callback){ bulkXHR.flush = function(callback){
foreach(bulkXHR.urls, function(queue, url){ foreach(bulkXHR.urls, function(queue, url){
var currentRequests = queue.requests, var currentRequests = queue.requests;
currentCallbacks = queue.callbacks;
if (currentRequests && currentRequests.length) { if (currentRequests && currentRequests.length) {
queue.requests = []; queue.requests = [];
queue.callbacks = []; queue.callbacks = [];
@ -267,14 +263,12 @@ angularService('$xhr.bulk', function($xhr, $error){
foreach(response, function(response, i){ foreach(response, function(response, i){
try { try {
if (response.status == 200) { if (response.status == 200) {
(currentCallbacks[i] || noop)(response.status, response.response); (currentRequests[i].callback || noop)(response.status, response.response);
} else { } else {
$error( $error(currentRequests[i], {status: response.status, body:response.response});
extend({}, currentRequests[i], {callback: currentCallbacks[i]}),
{status: response.status, body:response.response});
} }
} catch(e) { } catch(e) {
scope.$log.error(e); $log.error(e);
} }
}); });
(callback || noop)(); (callback || noop)();
@ -285,7 +279,7 @@ angularService('$xhr.bulk', function($xhr, $error){
}; };
this.$onEval(PRIORITY_LAST, bulkXHR.flush); this.$onEval(PRIORITY_LAST, bulkXHR.flush);
return bulkXHR; return bulkXHR;
}, {inject:['$xhr', '$xhr.error']}); }, {inject:['$xhr', '$xhr.error', '$log']});
angularService('$xhr.cache', function($xhr){ angularService('$xhr.cache', function($xhr){
var inflight = {}, self = this;; var inflight = {}, self = this;;