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<unknown module>* in onresize
> Can't find variable: tree
This commit is contained in:
EvB 2016-12-21 10:42:16 -05:00
parent 732ab67b57
commit 05a92f4389

View file

@ -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) { return function (scope, element, attr) {
var fn = $parse(attr.onresize); var fn = $parse(attr.onresize);
var notifyResized = function() { 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); fn(scope);
}); }, 0);
}; };
angular.element($window).on('resize', null, notifyResized); 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); angular.element($window).off('resize', null, notifyResized);
}); });
}; };
}); });