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

Commit cf5e463

Browse files
matskoIgorMinar
authored andcommitted
pref($animate): only trigger DOM callbacks if registered on the element being animated
BREAKING CHANGE: Both the `$animate:before` and `$animate:after` DOM events must be now registered prior to the $animate operation taking place. The `$animate:close` event can be registered anytime afterwards. DOM callbacks used to fired for each and every animation operation that occurs within the $animate service provided in the ngAnimate module. This may end up slowing down an application if 100s of elements are being inserted into the page. Therefore after this change callbacks are only fired if registered on the element being animated.
1 parent f288b8f commit cf5e463

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/ngAnimate/animate.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ angular.module('ngAnimate', ['ng'])
627627
return;
628628
}
629629

630+
var elementEvents = angular.element._data(node);
631+
elementEvents = elementEvents && elementEvents.events;
632+
630633
var animationLookup = (' ' + classes).replace(/\s+/g,'.');
631634
if (!parentElement) {
632635
parentElement = afterElement ? afterElement.parent() : element.parent();
@@ -822,29 +825,32 @@ angular.module('ngAnimate', ['ng'])
822825
}
823826

824827
function fireDOMCallback(animationPhase) {
825-
element.triggerHandler('$animate:' + animationPhase, {
826-
event : animationEvent,
827-
className : className
828-
});
828+
var eventName = '$animate:' + animationPhase;
829+
if(elementEvents && elementEvents[eventName] && elementEvents[eventName].length > 0) {
830+
$$asyncQueueBuffer(function() {
831+
element.triggerHandler(eventName, {
832+
event : animationEvent,
833+
className : className
834+
});
835+
});
836+
}
829837
}
830838

831839
function fireBeforeCallbackAsync() {
832-
$$asyncQueueBuffer(function() {
833-
fireDOMCallback('before');
834-
});
840+
fireDOMCallback('before');
835841
}
836842

837843
function fireAfterCallbackAsync() {
838-
$$asyncQueueBuffer(function() {
839-
fireDOMCallback('after');
840-
});
844+
fireDOMCallback('after');
841845
}
842846

843847
function fireDoneCallbackAsync() {
844-
$$asyncQueueBuffer(function() {
845-
fireDOMCallback('close');
846-
doneCallback && doneCallback();
847-
});
848+
fireDOMCallback('close');
849+
if(doneCallback) {
850+
$$asyncQueueBuffer(function() {
851+
doneCallback();
852+
});
853+
}
848854
}
849855

850856
//it is less complicated to use a flag than managing and cancelling

test/ngAnimate/animateSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,21 @@ describe("ngAnimate", function() {
15731573
expect(steps.shift()).toEqual(['after', 'ng-enter', 'enter']);
15741574
}));
15751575

1576+
it('should not fire DOM callbacks on the element being animated unless registered',
1577+
inject(function($animate, $rootScope, $compile, $sniffer, $rootElement, $timeout) {
1578+
1579+
$animate.enabled(true);
1580+
1581+
var element = jqLite('<div></div>');
1582+
$rootElement.append(element);
1583+
body.append($rootElement);
1584+
1585+
$animate.addClass(element, 'class');
1586+
$rootScope.$digest();
1587+
1588+
$timeout.verifyNoPendingTasks();
1589+
}));
1590+
15761591
it("should fire a done callback when provided with no animation",
15771592
inject(function($animate, $rootScope, $compile, $sniffer, $rootElement, $timeout) {
15781593

0 commit comments

Comments
 (0)