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

Commit 9b7b0fc

Browse files
committed
Adding ng-jq functionality, the ability to choose your angular.element library or force jqLite even if jquery is available.
1 parent 28fe446 commit 9b7b0fc

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/Angular.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,36 @@ function equals(o1, o2) {
767767
return false;
768768
}
769769

770+
/**
771+
* @ngdoc directive
772+
* @name ng.directive:ngJq
773+
*
774+
* @element html
775+
* @param {string} (optional) the name of the library available under `window` to be used for angular.element
776+
*
777+
* @description
778+
*
779+
* Use this directive to force the angular.element library. This should be used to force
780+
* either jqLite by leaving ng-jq blank or setting the name of the jquery variable under window
781+
* (eg. jQuery).
782+
*
783+
* This directive is global for the whole of the angular library, hence the directive can only
784+
* be added to the top-most HTML element.
785+
*
786+
*/
787+
var jq = function () {
788+
if (isDefined(jq.name_)) return jq.name_;
789+
790+
var el = document.querySelector('[ng-jq]') || document.querySelector('[data-ng-jq]');
791+
var name = undefined;
792+
793+
if (el) {
794+
name = el.getAttribute('ng-jq') || el.getAttribute('data-ng-jq') || '';
795+
}
796+
797+
return (jq.name_ = name);
798+
};
799+
770800

771801
function concat(array1, array2, index) {
772802
return array1.concat(slice.call(array2, index));
@@ -1147,7 +1177,7 @@ function snake_case(name, separator){
11471177

11481178
function bindJQuery() {
11491179
// bind to jQuery if present;
1150-
jQuery = window.jQuery;
1180+
jQuery = jq() ? window[jq()] : window.jQuery;
11511181
// reset to jQuery or default to us.
11521182
if (jQuery) {
11531183
jqLite = jQuery;

test/AngularSpec.js

+51
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,57 @@ describe('angular', function() {
347347
});
348348
});
349349

350+
describe('jq', function () {
351+
352+
var element;
353+
354+
beforeEach(function () {
355+
element = document.createElement('html');
356+
});
357+
358+
afterEach(function () {
359+
jq.name_ = undefined;
360+
delete jq.name_;
361+
});
362+
363+
it('should return undefined when jq is not set (the default)', function () {
364+
expect(jq()).toBe(undefined);
365+
});
366+
367+
it('should return empty string when jq is enabled manually via [ng-jq] with empty string', function () {
368+
element.setAttribute('ng-jq', '');
369+
spyOn(document, 'querySelector').andCallFake(function (selector) {
370+
if (selector == '[ng-jq]') return element;
371+
});
372+
expect(jq()).toBe('');
373+
});
374+
375+
it('should return empty string when jq is enabled manually via [data-ng-jq] with empty string', function () {
376+
element.setAttribute('data-ng-jq', '');
377+
spyOn(document, 'querySelector').andCallFake(function (selector) {
378+
if (selector == '[data-ng-jq]') return element;
379+
});
380+
expect(jq()).toBe('');
381+
expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]');
382+
});
383+
384+
it('should return "jquery" when jq is enabled manually via [ng-jq] with value "jquery"', function () {
385+
element.setAttribute('ng-jq', 'jquery');
386+
spyOn(document, 'querySelector').andCallFake(function (selector) {
387+
if (selector == '[ng-jq]') return element;
388+
});
389+
expect(jq()).toBe('jquery');
390+
});
391+
392+
it('should return "jquery" when jq is enabled manually via [data-ng-jq] with value "jquery"', function () {
393+
element.setAttribute('data-ng-jq', 'jquery');
394+
spyOn(document, 'querySelector').andCallFake(function (selector) {
395+
if (selector == '[data-ng-jq]') return element;
396+
});
397+
expect(jq()).toBe('jquery');
398+
expect(document.querySelector).toHaveBeenCalledWith('[data-ng-jq]');
399+
});
400+
});
350401

351402
describe('parseKeyValue', function() {
352403
it('should parse a string into key-value pairs', function() {

0 commit comments

Comments
 (0)