From 99627d576104518870fea27ed21ec53da51751bf Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 6 Feb 2017 14:38:34 +0300 Subject: [PATCH] refactor($rootScope): items in asyncQueue are already $parsed, no need to call $eval --- src/ng/rootScope.js | 7 ++++--- test/ng/rootScopeSpec.js | 14 +++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 0f3abed88fc2..88edcf3faa31 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -786,12 +786,13 @@ function $RootScopeProvider() { current = target; // It's safe for asyncQueuePosition to be a local variable here because this loop can't - // be reentered recursively. Calling $digest from a function passed to $applyAsync would + // be reentered recursively. Calling $digest from a function passed to $evalAsync would // lead to a '$digest already in progress' error. for (var asyncQueuePosition = 0; asyncQueuePosition < asyncQueue.length; asyncQueuePosition++) { try { asyncTask = asyncQueue[asyncQueuePosition]; - asyncTask.scope.$eval(asyncTask.expression, asyncTask.locals); + fn = asyncTask.fn; + fn(asyncTask.scope, asyncTask.locals); } catch (e) { $exceptionHandler(e); } @@ -1025,7 +1026,7 @@ function $RootScopeProvider() { }); } - asyncQueue.push({scope: this, expression: $parse(expr), locals: locals}); + asyncQueue.push({scope: this, fn: $parse(expr), locals: locals}); }, $$postDigest: function(fn) { diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index 93f3dc9e6eef..3f5a444f4a58 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -1444,9 +1444,9 @@ describe('Scope', function() { expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue); expect(isolateScope.$$asyncQueue).toBeUndefined(); expect($rootScope.$$asyncQueue).toEqual([ - {scope: $rootScope, expression: $parse('rootExpression'), locals: undefined}, - {scope: childScope, expression: $parse('childExpression'), locals: undefined}, - {scope: isolateScope, expression: $parse('isolateExpression'), locals: undefined} + {scope: $rootScope, fn: $parse('rootExpression'), locals: undefined}, + {scope: childScope, fn: $parse('childExpression'), locals: undefined}, + {scope: isolateScope, fn: $parse('isolateExpression'), locals: undefined} ]); })); @@ -1499,6 +1499,14 @@ describe('Scope', function() { expect(log).toEqual(['eval-ed 1!', 'eval-ed 2!']); }); }); + + it('should not pass anything as `this` to scheduled functions', inject(function($rootScope) { + var this1 = {}; + var this2 = (function() { return this; })(); + $rootScope.$evalAsync(function() { this1 = this; }); + $rootScope.$digest(); + expect(this1).toEqual(this2); + })); });