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

Add .noConflict() functionality #8596

Open
wants to merge 2 commits into
base: v1.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ var /** holds major version number for IE or NaN for real browsers */
ngMinErr = minErr('ng'),

/** @name angular */
angular = window.angular || (window.angular = {}),
_angular = window.angular,
angular = window.angular = {},
angularModule,
nodeName_,
uid = ['0', '0', '0'];
Expand Down Expand Up @@ -1494,3 +1495,26 @@ function getBlockElements(nodes) {

return jqLite(elements);
}


/**
@ngdoc function
* @name angular.noConflict
* @function
*
* @description
* Restores the previous global value of angular and returns the current instance. Other libraries may already use the
* angular namespace. Or a previous version of angular is already loaded on the page. In these cases you may want to
* restore the previous namespace and keep a reference to angular.
*
* @return {Object} The current angular namespace
*/
function noConflict() {
var a = window.angular;

if(_angular) {
window.angular = _angular;
}

return a;
}
3 changes: 2 additions & 1 deletion src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function publishExternalAPI(angular){
'uppercase': uppercase,
'callbacks': {counter: 0},
'$$minErr': minErr,
'$$csp': csp
'$$csp': csp,
'noConflict': noConflict
});

angularModule = setupModuleLoader(window);
Expand Down
2 changes: 1 addition & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
url = url || $browser.url();

if (lowercase(method) == 'jsonp') {
var callbackId = '_' + (callbacks.counter++).toString(36);
var callbackId = '_' + Math.random().toString(36).substring(7);

Choose a reason for hiding this comment

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

According to the "birthday paradox", if you have 100,000 callbacks, you will have a 6% chance of a collision. (See https://instacalc.com/28845, with "number of days" = 78364164096 = 36^7, and "number of people" = 100000.) So, I think this needs to be a longer random number.

Copy link
Member

Choose a reason for hiding this comment

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

Since we only care for the callback id while the request is in flight, we care about the number of concurrent JSONP requests. If you had 100000 of them, then 6% callback id collision rate would be the least of your problems 😛

callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
callbacks[callbackId].called = true;
Expand Down