From 05a92f4389855f998f7579313ace74397c536c6f Mon Sep 17 00:00:00 2001 From: EvB Date: Wed, 21 Dec 2016 10:42:16 -0500 Subject: [PATCH] fix(js/onresize): make sync call async in angular Wrap sync callback for the resize event in a `$timeout` so that it gets called asyncronously. Fixes error where `$apply` was geting called before previous `$apply` call finished. Angular allows only one `$apply` call at a time. See Angular.js reference. > [Error: $rootScope:inprog](https://docs.angularjs.org/error/$rootScope/inprog) Fixes: > *Error* ldn/angular in O/< > error[$rootScope:inprog] http://errors.angularjs.org/1.5.3/$rootScope/inprog?p0=%24digest Likely fixes other errors we're seeing related to resize events such as > *ReferenceError* in onresize > Can't find variable: tree --- static/js/directives/onresize.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/static/js/directives/onresize.js b/static/js/directives/onresize.js index b45e6e606..11eaa44a3 100644 --- a/static/js/directives/onresize.js +++ b/static/js/directives/onresize.js @@ -1,14 +1,18 @@ /** - * Adds an onresize event attribtue that gets invokved when the size of the window changes. + * Adds an onresize event attribute that gets invokved when the size of the window changes. */ -angular.module('quay').directive('onresize', function ($window, $parse) { +angular.module('quay').directive('onresize', function ($window, $parse, $timeout) { return function (scope, element, attr) { var fn = $parse(attr.onresize); var notifyResized = function() { - scope.$apply(function () { + // Angular.js enforces only one call to $apply can run at a time. + // Use $timeout to make the scope update safe, even when called within another $apply block, + // by scheduling it on the call stack. + // See docs: https://docs.angularjs.org/error/$rootScope/inprog + $timeout(function () { fn(scope); - }); + }, 0); }; angular.element($window).on('resize', null, notifyResized); @@ -17,4 +21,4 @@ angular.module('quay').directive('onresize', function ($window, $parse) { angular.element($window).off('resize', null, notifyResized); }); }; -}); \ No newline at end of file +});