From adeeb528fe14fbd153497df6e1ea7827b30dd01f Mon Sep 17 00:00:00 2001 From: James Talmage Date: Sat, 10 Aug 2013 00:11:26 -0400 Subject: [PATCH] fix($q): promises should still resolve outside of the $digest/$apply phase Add $rootScope.$apply() call upon resolve/reject if outside a digest phase Closes #2431 --- src/ng/q.js | 23 +++++++++++++++++++---- src/ng/timeout.js | 7 +++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/ng/q.js b/src/ng/q.js index 307dc1b42896..37f2649d8efd 100644 --- a/src/ng/q.js +++ b/src/ng/q.js @@ -161,14 +161,29 @@ */ function $QProvider() { - this.$get = ['$rootScope', '$exceptionHandler', function($rootScope, $exceptionHandler) { - return qFactory(function(callback) { - $rootScope.$evalAsync(callback); + this.$get = ['$rootScope', '$exceptionHandler','$browser', function($rootScope, $exceptionHandler,$browser) { + return qFactory(function (callback) { + if($rootScope.$$phase){ + $rootScope.$evalAsync(callback); + } + else { + var timeoutId = $browser.defer(function(){ + timeoutId = false; + $rootScope.$apply(); + }); + $rootScope.$evalAsync(function(){ + if(timeoutId){ + //$digest was called before + $browser.defer.cancel(timeoutId); + timeoutId = null; + } + $rootScope.$eval(callback); + }); + } }, $exceptionHandler); }]; } - /** * Constructs a promise manager. * diff --git a/src/ng/timeout.js b/src/ng/timeout.js index 81d09e8944dc..2515254a80b4 100644 --- a/src/ng/timeout.js +++ b/src/ng/timeout.js @@ -2,10 +2,13 @@ function $TimeoutProvider() { - this.$get = ['$rootScope', '$browser', '$q', '$exceptionHandler', - function($rootScope, $browser, $q, $exceptionHandler) { + this.$get = ['$rootScope', '$browser', '$exceptionHandler', + function($rootScope, $browser, $exceptionHandler) { var deferreds = {}; + var $q = qFactory(function(callback){ + $rootScope.$evalAsync(callback); + },$exceptionHandler); /** * @ngdoc function