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

Commit 821d1a3

Browse files
Michael ChenThomasBurleson
Michael Chen
authored andcommitted
fix(virtual-repeat): Prevent nested calls to virtualRepeatUpdate_
Due to some browser issues, the $watchCollection callback that calls virtualRepeatUpdate_ sometimes fires in the middle of a virtualRepeatUpdate_ call. This can result in duplicate items showing up in the virtual repeat list. Fixes #4950. Closes #5009.
1 parent 8d30ccb commit 821d1a3

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/components/virtualRepeat/virtual-repeater.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
422422
/** @type {boolean} Whether this is the first time that items are rendered. */
423423
this.isFirstRender = true;
424424

425+
/**
426+
* @private {boolean} Whether the items in the list are already being updated. Used to prevent
427+
* nested calls to virtualRepeatUpdate_.
428+
*/
429+
this.isVirtualRepeatUpdating_ = false;
430+
425431
/** @type {number} Most recently seen length of items. */
426432
this.itemsLength = 0;
427433

@@ -543,7 +549,11 @@ VirtualRepeatController.prototype.containerUpdated = function() {
543549
this.unwatchItemSize_();
544550
this.sized = true;
545551
this.$scope.$watchCollection(this.repeatListExpression,
546-
angular.bind(this, this.virtualRepeatUpdate_));
552+
angular.bind(this, function(items, oldItems) {
553+
if (!this.isVirtualRepeatUpdating_) {
554+
this.virtualRepeatUpdate_(items, oldItems);
555+
}
556+
}));
547557
}
548558

549559
this.updateIndexes_();
@@ -574,6 +584,8 @@ VirtualRepeatController.prototype.getItemSize = function() {
574584
* @private
575585
*/
576586
VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItems) {
587+
this.isVirtualRepeatUpdating_ = true;
588+
577589
var itemsLength = items && items.length || 0;
578590
var lengthChanged = false;
579591

@@ -664,6 +676,8 @@ VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItem
664676

665677
this.startIndex = this.newStartIndex;
666678
this.endIndex = this.newEndIndex;
679+
680+
this.isVirtualRepeatUpdating_ = false;
667681
};
668682

669683

0 commit comments

Comments
 (0)