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

Commit 5b133af

Browse files
committedJul 22, 2015
feat($http): add XHR events configuration option
Allows for arbitrary event handling on requests.
1 parent 8bf5654 commit 5b133af

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed
 

‎src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ function $HttpProvider() {
11551155
}
11561156

11571157
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
1158-
config.withCredentials, config.responseType);
1158+
config.withCredentials, config.responseType, config.events);
11591159
}
11601160

11611161
return promise;

‎src/ng/httpBackend.js

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

2929
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
3030
// TODO(vojta): fix the signature
31-
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
31+
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, eventHandlers) {
3232
$browser.$$incOutstandingRequestCount();
3333
url = url || $browser.url();
3434

@@ -88,6 +88,20 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
8888
xhr.onerror = requestError;
8989
xhr.onabort = requestError;
9090

91+
if (eventHandlers) {
92+
forEach(eventHandlers, function(value, key) {
93+
if (key !== 'upload') {
94+
xhr.addEventListener(key, value);
95+
}
96+
});
97+
98+
if (eventHandlers.upload) {
99+
forEach(eventHandlers.upload, function(value, key) {
100+
xhr.upload.addEventListener(key, value);
101+
});
102+
}
103+
}
104+
91105
if (withCredentials) {
92106
xhr.withCredentials = true;
93107
}

‎src/ngMock/angular-mocks.js

+11
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,17 @@ function MockXhr() {
16831683
};
16841684

16851685
this.abort = angular.noop;
1686+
1687+
this.$$events = {};
1688+
this.addEventListener = function(name, listener) {
1689+
if (angular.isUndefined(this.$$events[name])) this.$$events[name] = [];
1690+
this.$$events[name].push(listener);
1691+
};
1692+
1693+
this.upload = {
1694+
$$events: {},
1695+
addEventListener: this.addEventListener
1696+
};
16861697
}
16871698

16881699

‎test/ng/httpBackendSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ describe('$httpBackend', function() {
214214
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
215215
});
216216

217+
it('should set up event listeners', function() {
218+
var progressFn = function() {};
219+
var uploadProgressFn = function() {};
220+
$backend('GET', '/url', null, callback, {}, null, null, null, {
221+
progress: progressFn,
222+
upload: {
223+
progress: uploadProgressFn
224+
}
225+
});
226+
xhr = MockXhr.$$lastInstance;
227+
expect(xhr.$$events.progress[0]).toBe(progressFn);
228+
expect(xhr.upload.$$events.progress[0]).toBe(uploadProgressFn);
229+
});
217230

218231
describe('responseType', function() {
219232

0 commit comments

Comments
 (0)