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

Commit

Permalink
fix(virtual-repeat): Prevent nested calls to virtualRepeatUpdate_
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Michael Chen authored and ThomasBurleson committed Nov 4, 2015
1 parent 8d30ccb commit 821d1a3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
/** @type {boolean} Whether this is the first time that items are rendered. */
this.isFirstRender = true;

/**
* @private {boolean} Whether the items in the list are already being updated. Used to prevent
* nested calls to virtualRepeatUpdate_.
*/
this.isVirtualRepeatUpdating_ = false;

/** @type {number} Most recently seen length of items. */
this.itemsLength = 0;

Expand Down Expand Up @@ -543,7 +549,11 @@ VirtualRepeatController.prototype.containerUpdated = function() {
this.unwatchItemSize_();
this.sized = true;
this.$scope.$watchCollection(this.repeatListExpression,
angular.bind(this, this.virtualRepeatUpdate_));
angular.bind(this, function(items, oldItems) {
if (!this.isVirtualRepeatUpdating_) {
this.virtualRepeatUpdate_(items, oldItems);
}
}));
}

this.updateIndexes_();
Expand Down Expand Up @@ -574,6 +584,8 @@ VirtualRepeatController.prototype.getItemSize = function() {
* @private
*/
VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItems) {
this.isVirtualRepeatUpdating_ = true;

var itemsLength = items && items.length || 0;
var lengthChanged = false;

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

this.startIndex = this.newStartIndex;
this.endIndex = this.newEndIndex;

this.isVirtualRepeatUpdating_ = false;
};


Expand Down

0 comments on commit 821d1a3

Please sign in to comment.