From a004d487c4bb48b2bec19b60bc5ddc5244029be5 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 4 Feb 2011 16:42:21 -0800 Subject: allow jquery to be declared after angular in the script loading order --- src/Angular.js | 14 ++++++++++---- src/AngularPublic.js | 5 +++++ src/angular.suffix | 2 +- src/jqLite.js | 3 ++- src/scenario/angular.suffix | 2 +- test/testabilityPatch.js | 2 ++ 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 4187e9ad..b856aa24 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -83,17 +83,16 @@ var _undefined = undefined, PRIORITY_LAST = 99999, PRIORITY = {'FIRST': PRIORITY_FIRST, 'LAST': PRIORITY_LAST, 'WATCH':PRIORITY_WATCH}, Error = window.Error, - jQuery = window['jQuery'] || window['$'], // weirdness to make IE happy - _ = window['_'], /** holds major version number for IE or NaN for real browsers */ msie = parseInt((/msie (\d+)/.exec(lowercase(navigator.userAgent)) || [])[1], 10), - jqLite = jQuery || jqLiteWrap, + jqLite, // delay binding since jQuery could be loaded after us. + jQuery, // delay binding slice = Array.prototype.slice, push = Array.prototype.push, error = window[$console] ? bind(window[$console], window[$console]['error'] || noop) : noop, /** @name angular */ - angular = window[$angular] || (window[$angular] = {}), + angular = window[$angular] || (window[$angular] = {}), /** @name angular.markup */ angularTextMarkup = extensionMap(angular, 'markup'), /** @name angular.attrMarkup */ @@ -1006,6 +1005,7 @@ function angularInit(config){ } function angularJsConfig(document, config) { + bindJQuery(); var scripts = document.getElementsByTagName("script"), match; config = extend({ @@ -1028,3 +1028,9 @@ function angularJsConfig(document, config) { } return config; } + +function bindJQuery(){ + // bind to jQuery if present; + jQuery = window.jQuery; + angular.element = jqLite = jQuery || jqLiteWrap; +} diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 4654acb1..b62483f8 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -46,3 +46,8 @@ extend(angular, { 'isArray': isArray }); +//try to bind to jquery now so that one can write angular.element().read() +//but we will rebind on bootstrap again. +bindJQuery(); + + diff --git a/src/angular.suffix b/src/angular.suffix index 3da27dc0..fcd3e577 100644 --- a/src/angular.suffix +++ b/src/angular.suffix @@ -1,5 +1,5 @@ - jqLite(document).ready(function(){ + jqLiteWrap(document).ready(function(){ angularInit(angularJsConfig(document)); }); diff --git a/src/jqLite.js b/src/jqLite.js index b607c095..ad7734c9 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -97,7 +97,8 @@ JQLite.prototype = { } this.bind('DOMContentLoaded', trigger); // works for modern browsers and IE9 - jqLite(window).bind('load', trigger); // fallback to window.onload for others + // we can not use jqLite since we are not done loading and jQuery could be loaded later. + new JQLite(window).bind('load', trigger); // fallback to window.onload for others }, bind: function(type, fn){ diff --git a/src/scenario/angular.suffix b/src/scenario/angular.suffix index f3ef9f0d..3ab796d2 100644 --- a/src/scenario/angular.suffix +++ b/src/scenario/angular.suffix @@ -1,6 +1,6 @@ var $scenario = new angular.scenario.Runner(window); - jqLite(document).ready(function() { + jqLiteWrap(document).ready(function() { angularScenarioInit($scenario, angularJsConfig(document)); }); diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index e0086b8a..32272a4d 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -26,6 +26,8 @@ if (window.jstestdriver) { beforeEach(function(){ // This is to reset parsers global cache of expressions. compileCache = {}; + // reset to jQuery or default to us. + bindJQuery(); this.addMatchers({ toBeInvalid: function(){ var element = jqLite(this.actual); -- cgit v1.2.3