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"
This commit is contained in:
Lorenz Hübschle-Schneider 2014-04-28 13:40:27 +01:00
parent 26d43fa694
commit 1d2e5f1d0b
1 changed files with 14 additions and 16 deletions

View File

@ -491,24 +491,22 @@ function($rootScope,
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', '$log', 'models', 'connection', function ($rootScope, $scope, $store, $timeout, $log, models, connection) { 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 () { $rootScope.countWatchers = function () {
var root = $(document.getElementsByTagName('body')); var q = [$rootScope], watchers = 0, scope;
var watchers = []; while (q.length > 0) {
scope = q.pop();
var f = function (element) { if (scope.$$watchers) {
if (element.data().hasOwnProperty('$scope')) { watchers += scope.$$watchers.length;
angular.forEach(element.data().$scope.$$watchers, function (watcher) {
watchers.push(watcher);
});
} }
if (scope.$$childHead) {
angular.forEach(element.children(), function (childElement) { q.push(scope.$$childHead);
f($(childElement)); }
}); if (scope.$$nextSibling) {
}; q.push(scope.$$nextSibling);
}
f(root); }
console.log(watchers.length); console.log(watchers);
}; };