mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-23 09:50:24 +00:00
- split up services into files under src/service - split up specs into files under test/service - rewrite all specs so that they don't depend on one global forEach - get rid of obsolete code and tests in ng:switch - rename mock $log spec from "$log" to "$log mock"
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
/**
|
|
* @workInProgress
|
|
* @ngdoc service
|
|
* @name angular.service.$xhr.bulk
|
|
* @requires $xhr
|
|
* @requires $xhr.error
|
|
* @requires $log
|
|
*
|
|
* @description
|
|
*
|
|
* @example
|
|
*/
|
|
angularServiceInject('$xhr.bulk', function($xhr, $error, $log){
|
|
var requests = [],
|
|
scope = this;
|
|
function bulkXHR(method, url, post, callback) {
|
|
if (isFunction(post)) {
|
|
callback = post;
|
|
post = _null;
|
|
}
|
|
var currentQueue;
|
|
forEach(bulkXHR.urls, function(queue){
|
|
if (isFunction(queue.match) ? queue.match(url) : queue.match.exec(url)) {
|
|
currentQueue = queue;
|
|
}
|
|
});
|
|
if (currentQueue) {
|
|
if (!currentQueue.requests) currentQueue.requests = [];
|
|
currentQueue.requests.push({method: method, url: url, data:post, callback:callback});
|
|
} else {
|
|
$xhr(method, url, post, callback);
|
|
}
|
|
}
|
|
bulkXHR.urls = {};
|
|
bulkXHR.flush = function(callback){
|
|
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);
|
|
}
|
|
} catch(e) {
|
|
$log.error(e);
|
|
}
|
|
});
|
|
(callback || noop)();
|
|
});
|
|
scope.$eval();
|
|
}
|
|
});
|
|
};
|
|
this.$onEval(PRIORITY_LAST, bulkXHR.flush);
|
|
return bulkXHR;
|
|
}, ['$xhr', '$xhr.error', '$log']);
|