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

Commit 56c861c

Browse files
chrisirhcpetebacondarwin
authored andcommitted
feat($http): support handling additional XHR events
Closes #11547 Closes #1934
1 parent 451e0f6 commit 56c861c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/ng/http.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ function $HttpProvider() {
801801
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
802802
* HTTP headers to send to the server. If the return value of a function is null, the
803803
* header will not be sent. Functions accept a config object as an argument.
804+
* - **events** - `{Object}` - Event listeners to be bound to the XMLHttpRequest object.
805+
* To bind events to the XMLHttpRequest upload object, nest it under the upload key.
804806
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
805807
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
806808
* - **transformRequest** –
@@ -1259,7 +1261,7 @@ function $HttpProvider() {
12591261
}
12601262

12611263
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
1262-
config.withCredentials, config.responseType);
1264+
config.withCredentials, config.responseType, config.events);
12631265
}
12641266

12651267
return promise;

src/ng/httpBackend.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function $HttpBackendProvider() {
5454

5555
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
5656
// TODO(vojta): fix the signature
57-
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
57+
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, eventHandlers) {
5858
$browser.$$incOutstandingRequestCount();
5959
url = url || $browser.url();
6060

@@ -114,6 +114,20 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
114114
xhr.onerror = requestError;
115115
xhr.onabort = requestError;
116116

117+
if (eventHandlers) {
118+
forEach(eventHandlers, function(value, key) {
119+
if (key !== 'upload') {
120+
xhr.addEventListener(key, value);
121+
}
122+
});
123+
124+
if (eventHandlers.upload) {
125+
forEach(eventHandlers.upload, function(value, key) {
126+
xhr.upload.addEventListener(key, value);
127+
});
128+
}
129+
}
130+
117131
if (withCredentials) {
118132
xhr.withCredentials = true;
119133
}

src/ngMock/angular-mocks.js

+11
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,17 @@ function MockXhr() {
20092009
};
20102010

20112011
this.abort = angular.noop;
2012+
2013+
this.$$events = {};
2014+
this.addEventListener = function(name, listener) {
2015+
if (angular.isUndefined(this.$$events[name])) this.$$events[name] = [];
2016+
this.$$events[name].push(listener);
2017+
};
2018+
2019+
this.upload = {
2020+
$$events: {},
2021+
addEventListener: this.addEventListener
2022+
};
20122023
}
20132024

20142025

test/ng/httpBackendSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ describe('$httpBackend', function() {
241241
});
242242

243243

244+
it('should set up event listeners', function() {
245+
var progressFn = function() {};
246+
var uploadProgressFn = function() {};
247+
$backend('GET', '/url', null, callback, {}, null, null, null, {
248+
progress: progressFn,
249+
upload: {
250+
progress: uploadProgressFn
251+
}
252+
});
253+
xhr = MockXhr.$$lastInstance;
254+
expect(xhr.$$events.progress[0]).toBe(progressFn);
255+
expect(xhr.upload.$$events.progress[0]).toBe(uploadProgressFn);
256+
});
257+
258+
244259
describe('responseType', function() {
245260

246261
it('should set responseType and return xhr.response', function() {

0 commit comments

Comments
 (0)