From 1d2e5f1d0b31df34fe5a0c6f66dc938b9a16ed19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 28 Apr 2014 13:40:27 +0100 Subject: [PATCH] Better countWatchers The old one missed a few, two on the input and the one in the title. This version also doesn't use jQuery From: http://stackoverflow.com/a/18539624 by StackOverflow user "plantian" --- js/glowingbear.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/js/glowingbear.js b/js/glowingbear.js index 84bfbdc..d4d83b9 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -491,24 +491,22 @@ function($rootScope, weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', '$log', 'models', 'connection', function ($rootScope, $scope, $store, $timeout, $log, models, connection) { + // From: http://stackoverflow.com/a/18539624 by StackOverflow user "plantian" $rootScope.countWatchers = function () { - var root = $(document.getElementsByTagName('body')); - var watchers = []; - - var f = function (element) { - if (element.data().hasOwnProperty('$scope')) { - angular.forEach(element.data().$scope.$$watchers, function (watcher) { - watchers.push(watcher); - }); + var q = [$rootScope], watchers = 0, scope; + while (q.length > 0) { + scope = q.pop(); + if (scope.$$watchers) { + watchers += scope.$$watchers.length; } - - angular.forEach(element.children(), function (childElement) { - f($(childElement)); - }); - }; - - f(root); - console.log(watchers.length); + if (scope.$$childHead) { + q.push(scope.$$childHead); + } + if (scope.$$nextSibling) { + q.push(scope.$$nextSibling); + } + } + console.log(watchers); };