Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($scope): $evalAsync executes on the right scope
Browse files Browse the repository at this point in the history
Executes $evalAsync at the scope that the call was made

Closes: #3548
  • Loading branch information
lgalfaso authored and btford committed Oct 2, 2013
1 parent 4041482 commit 10cc1a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ function $RootScopeProvider(){
dirty, ttl = TTL,
next, current, target = this,
watchLog = [],
logIdx, logMsg;
logIdx, logMsg, asyncTask;

beginPhase('$digest');

Expand All @@ -511,7 +511,8 @@ function $RootScopeProvider(){

while(asyncQueue.length) {
try {
current.$eval(asyncQueue.shift());
asyncTask = asyncQueue.shift();
asyncTask.scope.$eval(asyncTask.expression);
} catch (e) {
$exceptionHandler(e);
}
Expand Down Expand Up @@ -704,7 +705,7 @@ function $RootScopeProvider(){
});
}

this.$$asyncQueue.push(expr);
this.$$asyncQueue.push({scope: this, expression: expr});
},

$$postDigest : function(fn) {
Expand Down
16 changes: 15 additions & 1 deletion test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,17 @@ describe('Scope', function() {
expect($rootScope.log).toBe('12');
}));

it('should run async expressions in their proper context', inject(function ($rootScope) {
var child = $rootScope.$new();
$rootScope.ctx = 'root context';
$rootScope.log = '';
child.ctx = 'child context';
child.log = '';
child.$evalAsync('log=ctx');
$rootScope.$digest();
expect($rootScope.log).toBe('');
expect(child.log).toBe('child context');
}));

it('should operate only with a single queue across all child and isolate scopes', inject(function($rootScope) {
var childScope = $rootScope.$new();
Expand All @@ -777,7 +788,10 @@ describe('Scope', function() {

expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect(isolateScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect($rootScope.$$asyncQueue).toEqual(['rootExpression', 'childExpression', 'isolateExpression']);
expect($rootScope.$$asyncQueue).toEqual([
{scope: $rootScope, expression: 'rootExpression'},
{scope: childScope, expression: 'childExpression'},
{scope: isolateScope, expression: 'isolateExpression'}]);
}));


Expand Down

0 comments on commit 10cc1a4

Please sign in to comment.