From 4c677a2843db765ae1b5db47131596d4a76957b9 Mon Sep 17 00:00:00 2001 From: James Morrin Date: Tue, 6 Nov 2012 17:35:47 -0500 Subject: [PATCH 1/2] feat(noConflict): restore previous namespace --- src/Angular.js | 18 ++++++++++++++++++ src/AngularPublic.js | 3 ++- test/AngularSpec.js | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 63d04200aba8..bfee31d3187d 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -58,12 +58,30 @@ var Error = window.Error, push = [].push, toString = Object.prototype.toString, + + _angular = window.angular, /** @name angular */ angular = window.angular || (window.angular = {}), 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. + * + * @returns the current angular namespace + */ +function noConflict() { + var a = window.angular; + window.angular = _angular; + return a; +} + /** * @ngdoc function * @name angular.forEach diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 61c77af34c2e..9928a12596f6 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -48,7 +48,8 @@ function publishExternalAPI(angular){ 'isDate': isDate, 'lowercase': lowercase, 'uppercase': uppercase, - 'callbacks': {counter: 0} + 'callbacks': {counter: 0}, + 'noConflict': noConflict }); angularModule = setupModuleLoader(window); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 476702968ec1..a4bd2198c4c0 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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(); + }); + + }); + }); From f25a863e9b657fc4d3121cf913758ca92411e63b Mon Sep 17 00:00:00 2001 From: James Morrin Date: Tue, 6 Nov 2012 17:35:47 -0500 Subject: [PATCH 2/2] feat(noConflict): restore previous namespace --- src/Angular.js | 18 ++++++++++++++++++ src/AngularPublic.js | 3 ++- test/AngularSpec.js | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 63d04200aba8..df311c856526 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -58,12 +58,30 @@ var Error = window.Error, push = [].push, toString = Object.prototype.toString, + + _angular = window.angular, /** @name angular */ angular = window.angular || (window.angular = {}), 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. + * + * @returns {Object} the current angular namespace + */ +function noConflict() { + var a = window.angular; + window.angular = _angular; + return a; +} + /** * @ngdoc function * @name angular.forEach diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 61c77af34c2e..9928a12596f6 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -48,7 +48,8 @@ function publishExternalAPI(angular){ 'isDate': isDate, 'lowercase': lowercase, 'uppercase': uppercase, - 'callbacks': {counter: 0} + 'callbacks': {counter: 0}, + 'noConflict': noConflict }); angularModule = setupModuleLoader(window); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 476702968ec1..a4bd2198c4c0 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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(); + }); + + }); + });