From 9d09c35cbc9c55ea25111118268af12848f3e1ad Mon Sep 17 00:00:00 2001 From: Ivan Borzenkov Date: Sat, 26 Apr 2014 11:30:27 +0400 Subject: [PATCH 1/2] unbind all watchers after start --- bindonce.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/bindonce.js b/bindonce.js index 3a6f43d..e9b2c1f 100644 --- a/bindonce.js +++ b/bindonce.js @@ -317,5 +317,38 @@ return bindonceDirective; }); - }) + }); + + bindonceModule.directive('bindTurn', function () + { + var findChild = function(scope) { + if (typeof scope.$stopUnbind != 'undefined') + return; + scope.$$listeners = {}; + scope.$$watchers = scope.$$asyncQueue = scope.$$postDigestQueue = []; + for(var cs = scope.$$childHead; cs; cs = cs.$$nextSibling) { + findChild(cs); + } + } + + return { + restrict: "AM", + scope: true, + link: function( $scope, $element, $attrs ) { + setTimeout(function() { + findChild($scope); + }, 0); + } + } + }); + bindonceModule.directive('bindPerm', function () + { + return { + scope: true, + link: function( $scope, $element ) { + $scope.$stopUnbind = true; + } + } + }); + })(); From 206044b6de53541fb319019c96bb16b07787be21 Mon Sep 17 00:00:00 2001 From: Ivan Borzenkov Date: Sat, 26 Apr 2014 12:04:23 +0400 Subject: [PATCH 2/2] switch watchers by flag --- bindonce.js | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/bindonce.js b/bindonce.js index e9b2c1f..7729579 100644 --- a/bindonce.js +++ b/bindonce.js @@ -321,11 +321,49 @@ bindonceModule.directive('bindTurn', function () { + var mainScope = null; + var watchVar = null; + var scopesList = {}; + + var sw = function(newVal, oldVal) { + if (newVal == oldVal) + return; + newVal ? bind() : unbind(); + }; + + var unbind = function() { + for(var i in scopesList) { + var item = scopesList[i]; + scopesList[i].listeners = item.scope.$$listeners; + scopesList[i].watchers = item.scope.$$watchers; + scopesList[i].asyncQueue = item.scope.$$asyncQueue; + scopesList[i].postDigestQueue = item.scope.$$postDigestQueue; + item.scope.$$listeners = {}; + item.scope.$$watchers = item.scope.$$asyncQueue = item.scope.$$postDigestQueue = []; + } + mainScope.$watch(watchVar, sw); + }; + + var bind = function() { + for(var i in scopesList) { + var item = scopesList[i]; + item.scope.$$listeners = item.listeners; + item.scope.$$watchers = item.watchers; + item.scope.$$asyncQueue = item.asyncQueue; + item.scope.$$postDigestQueue = item.postDigestQueue; + } + }; + var findChild = function(scope) { if (typeof scope.$stopUnbind != 'undefined') return; - scope.$$listeners = {}; - scope.$$watchers = scope.$$asyncQueue = scope.$$postDigestQueue = []; + scopesList[scope.$id] = { + listeners: scope.$$listeners, + watchers: scope.$$watchers, + asyncQueue: scope.$$asyncQueue, + postDigestQueue: scope.$$postDigestQueue, + scope: scope + }; for(var cs = scope.$$childHead; cs; cs = cs.$$nextSibling) { findChild(cs); } @@ -335,8 +373,13 @@ restrict: "AM", scope: true, link: function( $scope, $element, $attrs ) { + mainScope = $scope; + watchVar = $attrs.bindTurn; + mainScope.$watch(watchVar, sw); setTimeout(function() { findChild($scope); + if (mainScope[watchVar] == false) + unbind(); }, 0); } }