Created
March 29, 2016 14:21
-
-
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
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
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