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

feat(noConflict): restore previous namespace #1535

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,30 @@ var Error = window.Error,
push = [].push,
toString = Object.prototype.toString,


_angular = window.angular,
/** @name angular */
angular = window.angular || (window.angular = {}),
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't tried your patch, but looking at this line, it seems Angular will reuse any existing window.angular and extend it. This goes directly against the principle of returning the old value unchanged, and in fact turns noConflict() into a no-op.

angularModule,
nodeName_,
uid = ['0', '0', '0'];

/**
* @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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Per the contribution guidelines, all lines must wrap at 100 characters

*
* @returns {Object} the current angular namespace
*/
function noConflict() {
var a = window.angular;
window.angular = _angular;
return a;
}

/**
* @ngdoc function
* @name angular.forEach
Expand Down
3 changes: 2 additions & 1 deletion src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function publishExternalAPI(angular){
'isDate': isDate,
'lowercase': lowercase,
'uppercase': uppercase,
'callbacks': {counter: 0}
'callbacks': {counter: 0},
'noConflict': noConflict
});

angularModule = setupModuleLoader(window);
Expand Down
23 changes: 23 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,27 @@ describe('angular', function() {
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
}));
});

describe('noConflict', function() {
var globalAngular;
beforeEach(function() {
globalAngular = angular;
});

afterEach(function() {
angular = globalAngular;
});

it('should return angular', function() {
var a = angular.noConflict();
expect(a).toBe(globalAngular);
});

it('should restore original angular', function() {
var a = angular.noConflict();
expect(angular).toBeUndefined();
});

});

});