From 12ba6cec4fb79521101744e02a7e09f9fbb591c4 Mon Sep 17 00:00:00 2001 From: James Morrin Date: Tue, 6 Nov 2012 17:35:47 -0500 Subject: feat(noConflict): restore previous angular namespace reference --- src/Angular.js | 20 ++++++++++++++++++++ src/AngularPublic.js | 3 ++- test/AngularSpec.js | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 5195489e..6ea5d1ad 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -57,12 +57,32 @@ var /** holds major version number for IE or NaN for real browsers */ 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. + * + * @return {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 61c77af3..9928a125 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 e734decf..aed191e7 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -737,4 +737,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(); + }); + + }); + }); -- cgit v1.2.3