Created
February 3, 2016 08:45
-
-
Save MagnusThor/28159826256afaeda24c to your computer and use it in GitHub Desktop.
Count scope.$watch using data-wcount="true" attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Watchers = { | |
getWatchers : function(root) { | |
root = angular.element(root || document.documentElement); | |
function getElemWatchers(element) { | |
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope); | |
var scopeWatchers = getWatchersFromScope(element.data().$scope); | |
var watchers = scopeWatchers.concat(isolateWatchers); | |
angular.forEach(element.children(), function (childElement) { | |
watchers = watchers.concat(getElemWatchers(angular.element(childElement))); | |
}); | |
return watchers; | |
} | |
function getWatchersFromScope(scope) { | |
if (scope) { | |
return scope.$$watchers || []; | |
} else { | |
return []; | |
} | |
} | |
return getElemWatchers(root); | |
}, | |
checkWatchers: function (ctx) { | |
var self = this; | |
var createBubble = function(parent, numOfWatchers) { | |
var boundingBox = parent.getBoundingClientRect(); | |
var bubble = document.createElement("mark"); | |
bubble.style.position = "absolute"; | |
bubble.textContent = numOfWatchers; | |
bubble.style.backgroundColor = "#ff0000"; | |
bubble.title = parent.id + " = " + numOfWatchers; | |
bubble.style.top = boundingBox.top + "px"; | |
bubble.style.right = boundingBox.right + "px"; | |
bubble.classList.add("wcount"); | |
parent.appendChild(bubble); | |
}; | |
if (!ctx) ctx = document.body; | |
var nodes = ctx.querySelectorAll("[data-wcount]"); | |
for (var i = 0; i < nodes.length; i++) { | |
var node = nodes[i]; | |
var c = self.getWatchers(node); | |
node.setAttribute("title", c.length); | |
node.style.backgroundColor = "#dadada"; | |
createBubble(node, c.length); | |
}; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment