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

fix($jsonpCallbacks): do not overwrite callbacks added by other apps #14824

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions src/ng/jsonpCallbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
var $jsonpCallbacksProvider = function() {
this.$get = ['$window', function($window) {
var counter = 0;
$window.angular.callbacks = {};
var callbackMap = {};
var callbacks = $window.angular.callbacks = $window.angular.callbacks || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope 😃

This is basically doing:

foo = foo || {};   // foo <--- $window.angular.callbacks
var bar = foo;   // bar  <--- callbacks

The idea is that if $window.angular.callbacks has already been initialized (e.g. by another app running on the same page or by a different instance of $jsonpCallbacks created by a different injector), it should not be re-initialized/overwritten.

callbacks.$$counter = callbacks.$$counter || 0;

function createCallback(callbackId) {
var callback = function(data) {
Expand All @@ -35,10 +35,10 @@ var $jsonpCallbacksProvider = function() {
* to pass to the server, which will be used to call the callback with its payload in the JSONP response.
*/
createCallback: function(url) {
var callbackId = '_' + (counter++).toString(36);
var callbackId = '_' + (callbacks.$$counter++).toString(36);
var callbackPath = 'angular.callbacks.' + callbackId;
var callback = createCallback(callbackId);
callbackMap[callbackPath] = $window.angular.callbacks[callbackId] = callback;
callbackMap[callbackPath] = callbacks[callbackId] = callback;
return callbackPath;
},
/**
Expand Down Expand Up @@ -75,7 +75,7 @@ var $jsonpCallbacksProvider = function() {
*/
removeCallback: function(callbackPath) {
var callback = callbackMap[callbackPath];
delete $window.angular.callbacks[callback.id];
delete callbacks[callback.id];
delete callbackMap[callbackPath];
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,7 @@ angular.mock.$RootScopeDecorator = ['$delegate', function($delegate) {
angular.forEach(angular.callbacks, function(val, key) {
delete angular.callbacks[key];
});
angular.callbacks.counter = 0;
angular.callbacks.$$counter = 0;
};

(window.beforeEach || window.setup)(module.$$beforeEach);
Expand Down
13 changes: 13 additions & 0 deletions test/ng/jsonpCallbacksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ describe('$jsonpCallbacks', function() {
$jsonpCallbacks.createCallback('http://some.dummy.com/jsonp/request');
expect($window.angular.callbacks._3).toEqual(jasmine.any(Function));
}));

it('should produce unique callback paths across multiple instances', function() {
var $jsonpCallbacks1 = angular.injector(['ng', 'ngMock']).get('$jsonpCallbacks');
var $jsonpCallbacks2 = angular.injector(['ng', 'ngMock']).get('$jsonpCallbacks');

var path1 = $jsonpCallbacks1.createCallback('http://some.dummy.com/jsonp/request');
var path2 = $jsonpCallbacks2.createCallback('http://some.dummy.com/jsonp/request');

expect(path1).toBe('angular.callbacks._0');
expect(path2).toBe('angular.callbacks._1');
expect(angular.callbacks._0).toBeDefined();
expect(angular.callbacks._1).toBeDefined();
});
});


Expand Down