Skip to content

Instantly share code, notes, and snippets.

@timelf123
Created March 29, 2016 14:21
Show Gist options
  • Save timelf123/e86366583e6c3079febc to your computer and use it in GitHub Desktop.
Save timelf123/e86366583e6c3079febc to your computer and use it in GitHub Desktop.
Pauses all the children's $$watchers if the expression on the attribute evaluates to true, on false it restores any backed up $$watchers http://stackoverflow.com/a/29840248/411284
app.directive('pauseChildrenWatchersIf', function(){
return {
link: function (scope, element, attrs) {
scope.$watch(attrs.pauseChildrenWatchersIf, function (newVal) {
if (newVal === undefined) {
return;
}
if (newVal) {
toggleChildrenWatchers(element, true)
} else {
toggleChildrenWatchers(element, false)
}
});
function toggleChildrenWatchers(element, pause) {
angular.forEach(element.children(), function (childElement) {
toggleAllWatchers(angular.element(childElement), pause);
});
}
function toggleAllWatchers(element, pause) {
var data = element.data();
if (data.hasOwnProperty('$scope') && data.$scope.hasOwnProperty('$$watchers') && data.$scope.$$watchers) {
if (pause) {
data._bk_$$watchers = [];
$.each(data.$scope.$$watchers, function (i, watcher) {
data._bk_$$watchers.push($.extend(true, {}, watcher))
});
data.$scope.$$watchers = [];
} else {
if (data.hasOwnProperty('_bk_$$watchers')) {
$.each(data._bk_$$watchers, function (i, watcher) {
data.$scope.$$watchers.push($.extend(true, {}, watcher))
});
}
}
}
toggleChildrenWatchers(element, pause);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment