Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
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
10 changes: 10 additions & 0 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
this.blocks = {};
/** @type {Array<!VirtualRepeatController.Block>} A pool of presently unused blocks. */
this.pooledBlocks = [];

$scope.$on('$destroy', angular.bind(this, this.cleanupBlocks_));
}


Expand Down Expand Up @@ -536,6 +538,14 @@ VirtualRepeatController.prototype.link_ =
};


/** @private Cleans up unused blocks. */
VirtualRepeatController.prototype.cleanupBlocks_ = function() {
angular.forEach(this.pooledBlocks, function cleanupBlock(block) {
block.element.remove();
});
};


/** @private Attempts to set itemSize by measuring a repeated element in the dom */
VirtualRepeatController.prototype.readItemSize_ = function() {
if (this.itemSize) {
Expand Down
21 changes: 21 additions & 0 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,27 @@ describe('<md-virtual-repeat>', function() {
}));
});

describe('when container scope is destroyed', function() {

it('should clean up unused blocks', function() {
createRepeater();
var containerCtrl = component.controller('mdVirtualRepeatContainer');
scope.items = createItems(NUM_ITEMS);
scope.$apply();

scope.items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
scope.$apply();

scope.$destroy();

var dataCount = 0;
angular.forEach(containerCtrl.repeater.pooledBlocks, function(block) {
dataCount += Object.keys(block.element.data()).length;
});
expect(dataCount).toBe(0);
});
});

/**
* Facade to access transform properly even when jQuery is used;
* since jQuery's css function is obtaining the computed style (not wanted)
Expand Down