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

Commit

Permalink
fix(showHide): Don't set up $md-resize $broadcasting $watcher until r…
Browse files Browse the repository at this point in the history
…ecieving $md-resize-enable Fixes #5760

Closes #6170
  • Loading branch information
kseamon authored and jelbourn committed Dec 9, 2015
1 parent 2ab3075 commit 2f18bb4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/components/showHide/showHide.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ function createDirective(name, targetValue) {
restrict: 'A',
multiElement: true,
link: function($scope, $element, $attr) {
$scope.$watch($attr[name], function(value) {
if (!!value === targetValue) {
$mdUtil.nextTick(function() {
$scope.$broadcast('$md-resize');
});
$mdUtil.dom.animator.waitTransitionEnd($element).then(function() {
$scope.$broadcast('$md-resize');
});
}
var unregister = $scope.$on('$md-resize-enable', function() {
unregister();

$scope.$watch($attr[name], function(value) {
if (!!value === targetValue) {
$mdUtil.nextTick(function() {
$scope.$broadcast('$md-resize');
});
$mdUtil.dom.animator.waitTransitionEnd($element).then(function() {
$scope.$broadcast('$md-resize');
});
}
});
});
}
};
Expand Down
38 changes: 38 additions & 0 deletions src/components/showHide/showHide.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('showHide', function() {
it('should notify when the node unhides', function() {
scope.hide = true;
var element = $compile('<div ng-hide="hide"></div>')(scope);
scope.$broadcast('$md-resize-enable');
scope.$apply();
expect(spy).not.toHaveBeenCalled();

Expand All @@ -41,6 +42,7 @@ describe('showHide', function() {
it('should not notify on hide', function() {
scope.hide = true;
var element = $compile('<div ng-hide="hide"></div>')(scope);
scope.$broadcast('$md-resize-enable');
scope.$apply();

// Expect no $broadcasts when hiding.
Expand All @@ -49,12 +51,30 @@ describe('showHide', function() {
scope.$apply();
expect(spy).not.toHaveBeenCalled();
});

it('should not notify when not activated', function() {
scope.hide = true;
var element = $compile('<div ng-hide="hide"></div>')(scope);
scope.$apply();
expect(spy).not.toHaveBeenCalled();

scope.hide = false;
scope.$apply();
$timeout.flush();
expect(spy).not.toHaveBeenCalled();

spy.calls.reset();
defered.resolve();
scope.$apply();
expect(spy).not.toHaveBeenCalled();
});
});

describe('ng-show', function() {
it('should notify when the node unhides', function() {
scope.show = false;
var element = $compile('<div ng-show="show"></div>')(scope);
scope.$broadcast('$md-resize-enable');
scope.$apply();
expect(spy).not.toHaveBeenCalled();

Expand All @@ -74,6 +94,7 @@ describe('showHide', function() {
it('should not notify on hide', function() {
scope.show = false;
var element = $compile('<div ng-show="show"></div>')(scope);
scope.$broadcast('$md-resize-enable');
scope.$apply();

// Expect no $broadcasts when hiding.
Expand All @@ -82,5 +103,22 @@ describe('showHide', function() {
scope.$apply();
expect(spy).not.toHaveBeenCalled();
});

it('should not notify when not activated', function() {
scope.show = false;
var element = $compile('<div ng-show="show"></div>')(scope);
scope.$apply();
expect(spy).not.toHaveBeenCalled();

scope.show = true;
scope.$apply();
$timeout.flush();
expect(spy).not.toHaveBeenCalled();

spy.calls.reset();
defered.resolve();
scope.$apply();
expect(spy).not.toHaveBeenCalled();
});
});
});
1 change: 1 addition & 0 deletions src/components/virtualRepeat/virtual-repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function VirtualRepeatContainerController(
jWindow.off('resize', debouncedUpdateSize);
});

$scope.$emit('$md-resize-enable');
$scope.$on('$md-resize', boundUpdateSize);
}));
}
Expand Down
11 changes: 11 additions & 0 deletions src/components/virtualRepeat/virtual-repeater.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ describe('<md-virtual-repeat>', function() {
return component[0].querySelectorAll('[md-virtual-repeat]');
}

it('should $emit $md-resize-enable at startup', function() {
var emitted = false;
scope.$on('$md-resize-enable', function() {
emitted = true;
});

createRepeater();

expect(emitted).toBe(true);
});

it('should render only enough items to fill the viewport + 3 (vertical)', function() {
createRepeater();
scope.items = createItems(NUM_ITEMS);
Expand Down

0 comments on commit 2f18bb4

Please sign in to comment.