From a4c88cad828640c727227cceccb3f5df7db1548a Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Wed, 18 Jul 2012 18:28:46 -0700 Subject: [PATCH] fixed issue that assumed that window.angular existing meant that the app was ready to instrument; now also check that angular.module('ng') is defined. --- js/inject/debug.js | 138 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 121 insertions(+), 17 deletions(-) diff --git a/js/inject/debug.js b/js/inject/debug.js index 3ab76ee..86e07a0 100644 --- a/js/inject/debug.js +++ b/js/inject/debug.js @@ -1,6 +1,44 @@ var inject = function () { document.head.appendChild((function () { - var fn = function (window) { + var fn = function bootstrap (window) { + + var ngLoaded = function () { + if (!window.angular) { + return false; + } + try { + window.angular.module('ng'); + } + catch (e) { + return false; + } + return true; + }; + + // TODO: remove needless recursion + if (!ngLoaded()) { + (function () { + // TODO: var name + var areWeThereYet = function (ev) { + if (ev.srcElement.tagName === 'SCRIPT') { + var oldOnload = ev.srcElement.onload; + ev.srcElement.onload = function () { + if (ngLoaded()) { + + document.removeEventListener('DOMNodeInserted', areWeThereYet); + bootstrap(window); + } + if (oldOnload) { + oldOnload.apply(this, arguments); + } + }; + } + }; + document.addEventListener('DOMNodeInserted', areWeThereYet); + }()); + return; + } + // do not patch twice if (window.__ngDebug) { return; @@ -10,26 +48,53 @@ var inject = function () { watchers: {}, timeline: [], watchExp: {}, - watchList: {} + watchList: {}, + deps: [] + }; + /* + var injector = angular.injector; + angular.injector = function () { + console.log(arguments); + var ret = injector.apply(this, arguments); + + + return ret; }; + */ + + var module = angular.module; + /* + angular.module = function () { + console.log(arguments); + return module.apply(this, arguments); + }; + */ + + /* + angular.providerHook(function (name, path, fn) { + var curDep = debug.deps; + + var i; + for (i = path.length - 1; i >= 0; i -= 1) { + if (!curDep[path[i]]) { + curDep[path[i]] = {}; + } + curDep = curDep[path[i]]; + } + + if ((path.length === 0 || path[0] !== name) && !curDep[name]) { + curDep[name] = {}; + } + + return fn(); + }); +*/ + var ng = angular.module('ng'); ng.config(function ($provide) { - // patch $injector - // --------------- - /* - $provide.decorator('$injector', - function ($delegate) { - console.log($delegate); - var get = $delegate.__proto__.get; - $delegate.__proto__.get = function () { - return get.apply(this, arguments); - }; - return $delegate; - }); - */ - $provide.decorator('$rootScope', function ($delegate) { + var watchFnToHumanReadableString = function (fn) { if (fn.exp) { return fn.exp.trim(); @@ -170,5 +235,44 @@ var inject = function () { // only inject if cookie is set if (document.cookie.indexOf('__ngDebug=true') != -1) { - document.addEventListener('DOMContentLoaded', inject); + document.addEventListener('DOMContentLoaded', inject); + (function () { + + + var hackBootstrap = function () { + var bootstrap = angular.bootstrap; + window.angular.bootstrap = function () { + inject(); + bootstrap.apply(this, arguments); + }; + }; + + // else, patch angular.bootstrap + if (window.angular) { + hackBootstrap(); + } else { + // TODO: the AngularJS script it being asynchronously loaded and manually bootstrapped. + // Not sure what I can do here + + // current strategy: run at DOMContentLoaded and hope for the best + document.addEventListener('DOMContentLoaded', function () { + var areWeThereYet = function (ev) { + if (ev.srcElement.tagName === 'SCRIPT') { + var oldOnload = ev.srcElement.onload; + ev.srcElement.onload = function () { + if (window.angular) { + document.removeEventListener('DOMNodeInserted', areWeThereYet); + hackBootstrap(); + } + if (oldOnload) { + oldOnload.apply(this, arguments); + } + }; + } + } + document.addEventListener('DOMNodeInserted', areWeThereYet); + }); + } + + }()); }