Skip to content

Commit 50cdede

Browse files
chrisirhcpetebacondarwin
authored andcommitted
feat($http): support handling additional XHR events
Closes angular#11547 Closes angular#1934
1 parent 8dc08fb commit 50cdede

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/ng/http.js

Lines changed: 3 additions & 1 deletion
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

Lines changed: 15 additions & 1 deletion
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,17 @@ function MockXhr() {
20102010
};
20112011

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

20152026

test/ng/httpBackendSpec.js

Lines changed: 15 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy