Skip to content

Commit

Permalink
feat(uiView): cache and test autoscroll expression
Browse files Browse the repository at this point in the history
Changes to follow ngInclude's behavior more closely (like #714):

- memorizes the autoscroll expression, since it's not supposed to be
  changed once the template is compiled, only the evaluated
  expression should change;
- the page won't autoscroll when the attribute isn't present, which
  is a breaking change from the current implementation.
  • Loading branch information
ysbaddaden committed Dec 24, 2013
1 parent 8bb9e27 commit ee26228
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/viewDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an
var viewScope, viewLocals,
name = attr[directive.name] || attr.name || '',
onloadExp = attr.onload || '',
autoscrollExp = attr.autoscroll,
animate = $animator && $animator(scope, attr),
initialView = transclude(scope);

Expand Down Expand Up @@ -123,7 +124,7 @@ function $ViewDirective( $state, $compile, $controller, $injector, $an

// TODO: This seems strange, shouldn't $anchorScroll listen for $viewContentLoaded if necessary?
// $anchorScroll might listen on event...
if (!angular.isDefined(attr.autoscroll) || (!attr.autoscroll || scope.$eval(attr.autoscroll))) {
if (angular.isDefined(autoscrollExp) && (!autoscrollExp || scope.$eval(autoscrollExp))) {
$anchorScroll();
}
}
Expand Down
14 changes: 8 additions & 6 deletions test/viewDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ describe('uiView', function () {
});

describe('autoscroll attribute', function () {
it('should autoscroll when unspecified', inject(function ($state, $q, $anchorScroll) {
it('should not autoscroll when unspecified', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view></div>')(scope));
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).toHaveBeenCalled();
expect($anchorScroll).not.toHaveBeenCalled();
}));

it('should autoscroll when expr is missing', inject(function ($state, $q, $anchorScroll) {
Expand All @@ -230,15 +230,17 @@ describe('uiView', function () {
expect($anchorScroll).toHaveBeenCalled();
}));

it('should autoscroll when truthy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="true"></div>')(scope));
it('should autoscroll when expression is truthy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="doAutoScroll"></div>')(scope));
scope.doAutoScroll = true;
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).toHaveBeenCalled();
}));

it('should not autoscroll when falsy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="false"></div>')(scope));
it('should not autoscroll when expression is falsy', inject(function ($state, $q, $anchorScroll) {
elem.append($compile('<div ui-view autoscroll="doAutoScroll()"></div>')(scope));
scope.doAutoScroll = function () { return false; };
$state.transitionTo(gState);
$q.flush();
expect($anchorScroll).not.toHaveBeenCalled();
Expand Down

0 comments on commit ee26228

Please sign in to comment.