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

Commit

Permalink
fix(virtualRepeat): Broken demos relating to size computation
Browse files Browse the repository at this point in the history
Fix test timeouts (something about the way I spied on debounce).

Closes #6167
  • Loading branch information
kseamon authored and jelbourn committed Dec 8, 2015
1 parent 36b03f2 commit 1013423
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,26 @@ function VirtualRepeatContainerController(
// make a best effort at re-measuring as it changes.
var boundUpdateSize = angular.bind(this, this.updateSize);

$$rAF(function() {
$$rAF(angular.bind(this, function() {
boundUpdateSize();

var debouncedUpdateSize = $mdUtil.debounce(boundUpdateSize, 10, null, false);
var jWindow = angular.element($window);

// Make one more attempt to get the size if it is 0.
// This is not by any means a perfect approach, but there's really no
// silver bullet here.
if (!this.size) {
debouncedUpdateSize();
}

jWindow.on('resize', debouncedUpdateSize);
$scope.$on('$destroy', function() {
jWindow.off('resize', debouncedUpdateSize);
});

$scope.$on('$md-resize', boundUpdateSize);
});
}));
}


Expand Down Expand Up @@ -187,6 +194,7 @@ VirtualRepeatContainerController.prototype.updateSize = function() {
this.size = this.isHorizontal()
? this.$element[0].clientWidth
: this.$element[0].clientHeight;

this.repeater && this.repeater.containerUpdated();
};

Expand Down
24 changes: 21 additions & 3 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('<md-virtual-repeat>', function() {
ITEM_SIZE = 10;

beforeEach(inject(function(
_$$rAF_, _$compile_, _$document_, _$mdUtil_, $rootScope, _$window_, _$material_) {
_$$rAF_, _$compile_, _$document_, _$mdUtil_, $rootScope, _$timeout_, _$window_, _$material_) {
repeater = angular.element(REPEATER_HTML);
container = angular.element(CONTAINER_HTML).append(repeater);
component = null;
Expand All @@ -31,6 +31,7 @@ describe('<md-virtual-repeat>', function() {
$mdUtil = _$mdUtil_;
$compile = _$compile_;
$document = _$document_;
$timeout = _$timeout_;
$window = _$window_;
scope = $rootScope.$new();
scope.startIndex = 0;
Expand Down Expand Up @@ -582,13 +583,30 @@ describe('<md-virtual-repeat>', function() {
expect(offsetter.children().length).toBe(43);
});

it('makes a second attempt to measure the size if it starts out at 0',
function() {
// Create the repeater before appending it to the body.
scope.items = createItems(100);
component = $compile(container)(scope);
$material.flushOutstandingAnimations();
angular.element($document[0].body).append(container);
offsetter = angular.element(component[0].querySelector('.md-virtual-repeat-offsetter'));

// Expect 3 children (0 + 3 extra).
expect(offsetter.children().length).toBe(3);

// Expect it to remeasure using debounce.
$timeout.flush();

// Expect 13 children (10 + 3 extra).
expect(offsetter.children().length).toBe(13);
});

/**
* Facade to access transform properly even when jQuery is used;
* since jQuery's css function is obtaining the computed style (not wanted)
*/
function getTransform(target) {
return target[0].style.webkitTransform || target.css('transform');
}


});

0 comments on commit 1013423

Please sign in to comment.