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

Commit 0027549

Browse files
Michel Boudreaumboudreau
Michel Boudreau
authored andcommitted
Adding ng-jq functionality, the ability to force jqLite or any other library by name to be used for angular.element
1 parent 0d42426 commit 0027549

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/Angular.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,35 @@ var csp = function() {
899899
return (csp.isActive_ = active);
900900
};
901901

902+
/**
903+
* @ngdoc directive
904+
* @name ng.directive:ngJq
905+
*
906+
* @element html
907+
* @param {string} (optional) the name of the library available under `window` to be used for angular.element
908+
*
909+
* @description
910+
*
911+
* Use this directive to force the angular.element library. This should be used to force
912+
* either jqLite by leaving ng-jq blank or setting the name of the jquery variable under window
913+
* (eg. jQuery).
914+
*
915+
* This directive is global for the whole of the angular library, hence the directive can only
916+
* be added to the top-most HTML element.
917+
*
918+
*/
919+
var jq = function() {
920+
if (isDefined(jq.name_)) return jq.name_;
902921

922+
var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]');
923+
var name = undefined;
924+
925+
if (el) {
926+
name = el.getAttribute('ng-jq') || el.getAttribute('data-ng-jq') || '';
927+
}
928+
929+
return (jq.name_ = name);
930+
};
903931

904932
function concat(array1, array2, index) {
905933
return array1.concat(slice.call(array2, index));
@@ -1472,7 +1500,9 @@ function bindJQuery() {
14721500
}
14731501

14741502
// bind to jQuery if present;
1475-
jQuery = window.jQuery;
1503+
var jqName = jq();
1504+
jQuery = jqName !== undefined ? window[jqName] : window.jQuery;
1505+
14761506
// Use jQuery if it exists with proper functionality, otherwise default to us.
14771507
// Angular 1.2+ requires jQuery 1.7+ for on()/off() support.
14781508
// Angular 1.3+ technically requires at least jQuery 2.1+ but it may work with older

test/AngularSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,58 @@ describe('angular', function() {
615615
});
616616

617617

618+
describe('jq', function() {
619+
var element, fakeQuery;
620+
621+
beforeEach(function() {
622+
element = document.createElement('html');
623+
});
624+
625+
afterEach(function() {
626+
jq.name_ = undefined;
627+
delete jq.name_;
628+
});
629+
630+
it('should return undefined when jq is not set, no jquery found (the default)', function() {
631+
expect(jq()).toBe(undefined);
632+
});
633+
634+
it('should return empty string when jq is enabled manually via [ng-jq] with empty string', function() {
635+
element.setAttribute('ng-jq', '');
636+
spyOn(document, 'querySelector').andCallFake(function(selector) {
637+
if (selector == '[ng-jq]') return element;
638+
});
639+
expect(jq()).toBe('');
640+
});
641+
642+
it('should return empty string when jq is enabled manually via [data-ng-jq] with empty string', function() {
643+
element.setAttribute('data-ng-jq', '');
644+
spyOn(document, 'querySelector').andCallFake(function(selector) {
645+
if (selector == '[data-ng-jq]') return element;
646+
});
647+
expect(jq()).toBe('');
648+
expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]');
649+
});
650+
651+
it('should return "jquery" when jq is enabled manually via [ng-jq] with value "jquery"', function() {
652+
element.setAttribute('ng-jq', 'jQuery');
653+
spyOn(document, 'querySelector').andCallFake(function(selector) {
654+
if (selector == '[ng-jq]') return element;
655+
});
656+
expect(jq()).toBe('jQuery');
657+
});
658+
659+
it('should return "jquery" when jq is enabled manually via [data-ng-jq] with value "jquery"', function() {
660+
element.setAttribute('data-ng-jq', 'jQuery');
661+
spyOn(document, 'querySelector').andCallFake(function(selector) {
662+
if (selector == '[data-ng-jq]') return element;
663+
});
664+
expect(jq()).toBe('jQuery');
665+
expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]');
666+
});
667+
});
668+
669+
618670
describe('parseKeyValue', function() {
619671
it('should parse a string into key-value pairs', function() {
620672
expect(parseKeyValue('')).toEqual({});

0 commit comments

Comments
 (0)