Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

fix(tabs): Initial tab selection #834

Closed
wants to merge 1 commit into from
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
14 changes: 9 additions & 5 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,22 @@ function($parse, $http, $templateCache, $compile) {
if (attrs.active) {
getActive = $parse(attrs.active);
setActive = getActive.assign;
scope.$parent.$watch(getActive, function updateActive(value) {
scope.active = !!value;
scope.$parent.$watch(getActive, function updateActive(value, oldVal) {
// Avoid re-initializing scope.active as it is already initialized
// below. (watcher is called async during init with value ===
// oldVal)
if (value !== oldVal) {
scope.active = !!value;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The watcher is evaluated asynchronously after initialization even when there are no changes to the watched expression.
There is a chance (if the $digest cycles and planets align...) that the active variable gets initialized a second time using the getActive (in the watcher for getActive). Since we're already setting active to getActive, and the active variable should now be carrying the truth. Avoid this re-initialization.

});
scope.active = getActive(scope.$parent);
} else {
setActive = getActive = angular.noop;
}

scope.$watch('active', function(active) {
// Note this watcher also initializes and assigns scope.active to the
// attrs.active expression.
setActive(scope.$parent, active);
if (active) {
tabsetCtrl.select(scope);
Expand All @@ -234,9 +241,6 @@ function($parse, $http, $templateCache, $compile) {
scope.$on('$destroy', function() {
tabsetCtrl.removeTab(scope);
});
if (scope.active) {
setActive(scope.$parent, true);
}


//We need to transclude later, once the content container is ready.
Expand Down
58 changes: 58 additions & 0 deletions src/tabs/test/tabsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,64 @@ describe('tabs', function() {

});

describe('basics with initial active tab', function() {

beforeEach(inject(function($compile, $rootScope) {
scope = $rootScope.$new();

function makeTab(active) {
return {
active: !!active,
select: jasmine.createSpy()
};
}
scope.tabs = [
makeTab(), makeTab(), makeTab(true), makeTab()
];
elm = $compile([
'<tabset>',
' <tab active="tabs[0].active" select="tabs[0].select()">',
' </tab>',
' <tab active="tabs[1].active" select="tabs[1].select()">',
' </tab>',
' <tab active="tabs[2].active" select="tabs[2].select()">',
' </tab>',
' <tab active="tabs[3].active" select="tabs[3].select()">',
' </tab>',
'</tabset>'
].join('\n'))(scope);
scope.$apply();
}));

function titles() {
return elm.find('ul.nav-tabs li');
}
function contents() {
return elm.find('div.tab-content div.tab-pane');
}

function expectTabActive(activeTab) {
var _titles = titles();
angular.forEach(scope.tabs, function(tab, i) {
if (activeTab === tab) {
expect(tab.active).toBe(true);
//It should only call select ONCE for each select
expect(tab.select).toHaveBeenCalled();
expect(_titles.eq(i)).toHaveClass('active');
expect(contents().eq(i)).toHaveClass('active');
} else {
expect(tab.active).toBe(false);
expect(_titles.eq(i)).not.toHaveClass('active');
}
});
}

it('should make tab titles and set active tab active', function() {
expect(titles().length).toBe(scope.tabs.length);
expectTabActive(scope.tabs[2]);
});
});

describe('ng-repeat', function() {

beforeEach(inject(function($compile, $rootScope) {
Expand Down