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

Commit fd38655

Browse files
committed
fix(): use angular.callbacks namespace for jsonp callbacks
Previously we used to put callbacks on the window object, but that causes problems on IE8 where it is not possible to delete properties from the window object
1 parent b9001e9 commit fd38655

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

src/Angular.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ var $$scope = '$scope',
101101
angularWidget = extensionMap(angular, 'widget', shivForIE),
102102
/** @name angular.module.ng */
103103
angularInputType = extensionMap(angular, 'inputType', lowercase),
104-
/** @name angular.module.ng */
105-
angularCallbacks = extensionMap(angular, 'callbacks'),
106104
nodeName_,
107105
uid = ['0', '0', '0'],
108106
DATE_ISOSTRING_LN = 24;

src/AngularPublic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function publishExternalAPI(angular){
4747
'version': version,
4848
'isDate': isDate,
4949
'lowercase': lowercase,
50-
'uppercase': uppercase
50+
'uppercase': uppercase,
51+
'callbacks': {counter: 0}
5152
});
5253

5354
angularModule.ng = ngModule;

src/service/httpBackend.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,29 @@ var XHR = window.XMLHttpRequest || function() {
1717
*/
1818
function $HttpBackendProvider() {
1919
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
20-
return createHttpBackend($browser, XHR, $browser.defer, $window, $document[0].body,
21-
$window.location.href.replace(':', ''));
20+
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks,
21+
$document[0].body, $window.location.href.replace(':', ''));
2222
}];
2323
}
2424

25-
function createHttpBackend($browser, XHR, $browserDefer, $window, body, locationProtocol) {
26-
var idCounter = 0;
27-
25+
function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locationProtocol) {
2826
// TODO(vojta): fix the signature
2927
return function(method, url, post, callback, headers, timeout) {
3028
$browser.$$incOutstandingRequestCount();
3129

3230
if (lowercase(method) == 'jsonp') {
33-
var callbackId = ('angular_' + Math.random() + '_' + (idCounter++)).replace(/\d\./, '');
34-
$window[callbackId] = function(data) {
35-
$window[callbackId].data = data;
31+
var callbackId = '_' + (callbacks.counter++).toString(36);
32+
callbacks[callbackId] = function(data) {
33+
callbacks[callbackId].data = data;
3634
};
3735

3836
var script = $browser.addJs(url.replace('JSON_CALLBACK', callbackId), null, function() {
39-
if ($window[callbackId].data) {
40-
completeRequest(callback, 200, $window[callbackId].data);
37+
if (callbacks[callbackId].data) {
38+
completeRequest(callback, 200, callbacks[callbackId].data);
4139
} else {
4240
completeRequest(callback, -2);
4341
}
44-
delete $window[callbackId];
42+
delete callbacks[callbackId];
4543
body.removeChild(script);
4644
});
4745
} else {

test/service/httpBackendSpec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('$httpBackend', function() {
22

3-
var $backend, $browser, $window,
3+
var $backend, $browser, callbacks,
44
xhr, fakeBody, callback;
55

66
// TODO(vojta): should be replaced by $defer mock
@@ -19,10 +19,10 @@ describe('$httpBackend', function() {
1919

2020

2121
beforeEach(inject(function($injector) {
22-
$window = {};
22+
callbacks = {};
2323
$browser = $injector.get('$browser');
2424
fakeBody = {removeChild: jasmine.createSpy('body.removeChild')};
25-
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, $window, fakeBody);
25+
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeBody);
2626
callback = jasmine.createSpy('done');
2727
}));
2828

@@ -135,7 +135,7 @@ describe('$httpBackend', function() {
135135
url = script.url.split('?cb=');
136136

137137
expect(url[0]).toBe('http://example.org/path');
138-
$window[url[1]]('some-data');
138+
callbacks[url[1]]('some-data');
139139
script.done();
140140

141141
expect(callback).toHaveBeenCalledOnce();
@@ -149,10 +149,10 @@ describe('$httpBackend', function() {
149149
var script = $browser.$$scripts.shift(),
150150
callbackId = script.url.split('?cb=')[1];
151151

152-
$window[callbackId]('some-data');
152+
callbacks[callbackId]('some-data');
153153
script.done();
154154

155-
expect($window[callbackId]).toBeUndefined();
155+
expect(callbacks[callbackId]).toBeUndefined();
156156
expect(fakeBody.removeChild).toHaveBeenCalledOnce();
157157
expect(fakeBody.removeChild).toHaveBeenCalledWith(script);
158158
});

0 commit comments

Comments
 (0)