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

feat(rootScope): allow $apply expressions to prevent unnecessary digest #8301

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ng/directive/ngEventDirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ forEach(
var fn = $parse(attr[directiveName], /* interceptorFn */ null, /* expensiveChecks */ true);
return function ngEventHandler(scope, element) {
element.on(eventName, function(event) {
var callback = function() {
fn(scope, {$event:event});
var callback = function(scope, locals) {
fn(scope, {
$event:event,
$abortApply: locals && locals.$abortApply,
$partialDigest: locals && locals.$partialDigest
});
};
if (forceAsyncEvents[eventName] && $rootScope.$$phase) {
scope.$evalAsync(callback);
Expand Down
25 changes: 19 additions & 6 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,18 +1022,31 @@ function $RootScopeProvider() {
* @returns {*} The result of evaluating the expression.
*/
$apply: function(expr) {
var abortApply = false;
var scopeForDigest = $rootScope;
var currentScope = this;

try {
beginPhase('$apply');
return this.$eval(expr);
return this.$eval(expr, {
$abortApply: function() {
abortApply = true;
},
$partialDigest: function(scope) {
scopeForDigest = scope || currentScope;
}
});
} catch (e) {
$exceptionHandler(e);
} finally {
clearPhase();
try {
$rootScope.$digest();
} catch (e) {
$exceptionHandler(e);
throw e;
if (!abortApply) {
try {
scopeForDigest.$digest();
} catch (e) {
$exceptionHandler(e);
throw e;
}
}
}
},
Expand Down
20 changes: 20 additions & 0 deletions test/ng/directive/ngClickSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ describe('ngClick', function() {
browserTrigger(element, 'click');
expect($rootScope.event).toBeDefined();
}));

it('should abort apply if abort argument is invoked', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="a = 1; $abortApply()">{{a}}</div>')($rootScope);
$rootScope.$digest();

browserTrigger(element, 'click');
expect(element.text()).toBe('');
}));

it('should digest locally if $partialDigest is invoked', inject(function($rootScope, $compile) {
var scope = $rootScope.$new();
element = $compile('<div ng-click="a = 1; $partialDigest()">{{a}}</div>')(scope);
$rootScope.$digest();

var watchSpy = jasmine.createSpy('watchSpy');
$rootScope.$watch(watchSpy);
browserTrigger(element, 'click');
expect(element.text()).toBe('1');
expect(watchSpy).not.toHaveBeenCalled();
}));
});
33 changes: 33 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,39 @@ describe('Scope', function() {
expect(log).toEqual('1');
}));

it('should abort apply if abort argument is invoked', inject(function($rootScope) {
var watchSpy = jasmine.createSpy('watchSpy');
$rootScope.$watch(watchSpy);
$rootScope.$apply('$abortApply()');
expect(watchSpy).not.toHaveBeenCalled();
}));

it('should perform digest locally if $partialDigest is invoked', inject(function($rootScope) {
var watchRootSpy = jasmine.createSpy('watchRootSpy');
var watchSpy = jasmine.createSpy('watchSpy');
var child = $rootScope.$new();

$rootScope.$watch(watchRootSpy);
child.$watch(watchSpy);
child.$apply('$partialDigest()');

expect(watchRootSpy).not.toHaveBeenCalled();
expect(watchSpy).toHaveBeenCalled();
}));

it('should perform digest on specific scope', inject(function($rootScope) {
var watchRootSpy = jasmine.createSpy('watchRootSpy');
var watchSpy = jasmine.createSpy('watchSpy');
var child = $rootScope.$new();
var grandChild = child.$new();

$rootScope.$watch(watchRootSpy);
child.$watch(watchSpy);
grandChild.$apply('$partialDigest($parent)');

expect(watchRootSpy).not.toHaveBeenCalled();
expect(watchSpy).toHaveBeenCalled();
}));

it('should catch exceptions', function() {
module(function($exceptionHandlerProvider) {
Expand Down