Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat($http) Add support for custom event hooks (progress, etc) #2725

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ function $HttpProvider() {
// if we won't have the response in cache, send the request to the backend
if (!cachedResp) {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
config.withCredentials, config.responseType, config.hooks);
}

return promise;
Expand Down
14 changes: 13 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function $HttpBackendProvider() {

function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, hooks) {
var status;
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();
Expand All @@ -59,9 +59,21 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
if (value) xhr.setRequestHeader(key, value);
});

// Add listeners for events such as progress
hooks = hooks || {};
var uploadHooks = hooks.upload || {};
delete hooks.upload;
forEach(Object.keys(hooks), function(event) {
xhr.addEventListener(event, hooks[event], false);
});
forEach(Object.keys(uploadHooks), function(event) {
xhr.upload.addEventListener(event, uploadHooks[event], false);
});

// In IE6 and 7, this might be called synchronously when xhr.send below is called and the
// response is in the cache. the promise api will ensure that to the app code the api is
// always async

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var responseHeaders = xhr.getAllResponseHeaders();
Expand Down