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

Commit

Permalink
fix(tabs): ensure tab selection only occurs once
Browse files Browse the repository at this point in the history
- Fix issue where select callback is fired twice due to compilation order when using dynamic tabs mixed with static tabs

Closes #3060
Closes #4230
Fixes #2883
  • Loading branch information
Casey Garland authored and wesleycho committed Aug 18, 2015
1 parent 790d6b9 commit 7d3ba1e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/tabs/tabs.js
Expand Up @@ -18,10 +18,15 @@ angular.module('ui.bootstrap.tabs', [])
if (tab.active && tab !== selectedTab) {
tab.active = false;
tab.onDeselect();
selectedTab.selectCalled = false;
}
});
selectedTab.active = true;
selectedTab.onSelect();
// only call select if it has not already been called
if (!selectedTab.selectCalled) {
selectedTab.onSelect();
selectedTab.selectCalled = true;
}
};

ctrl.addTab = function addTab(tab) {
Expand Down
45 changes: 38 additions & 7 deletions src/tabs/test/tabs.spec.js
Expand Up @@ -191,15 +191,12 @@ describe('tabs', function() {
});

describe('ng-repeat', function() {
beforeEach(inject(function($compile, $rootScope) {
var $compile, $rootScope;
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
scope = $rootScope.$new();

function makeTab(active) {
return {
active: !!active,
select: jasmine.createSpy()
};
}
scope.tabs = [
makeTab(), makeTab(), makeTab(true), makeTab()
];
Expand All @@ -214,6 +211,13 @@ describe('tabs', function() {
scope.$apply();
}));

function makeTab(active) {
return {
active: !!active,
select: jasmine.createSpy()
};
}

function titles() {
return elm.find('ul.nav-tabs li');
}
Expand Down Expand Up @@ -263,6 +267,33 @@ describe('tabs', function() {
scope.$apply();
expectTabActive(scope.tabs[2]);
});

it('should not select twice', function() {
elm.remove();
elm = null;
scope = $rootScope.$new();

scope.tabs = [
makeTab(), makeTab(), makeTab(true), makeTab()
];
scope.foo = {active: true};
scope.select = jasmine.createSpy();
elm = $compile([
'<tabset>',
' <tab ng-repeat="t in tabs" active="t.active" select="select()">',
' <tab-heading><b>heading</b> {{index}}</tab-heading>',
' content {{$index}}',
' </tab>',
' <tab active="foo.active" select="select()">',
' <tab-heading><b>heading</b> foo</tab-heading>',
' content foo',
' </tab>',
'</tabset>'
].join('\n'))(scope);
scope.$apply();

expect(scope.select.calls.count()).toBe(1);
});
});

describe('advanced tab-heading element', function() {
Expand Down

0 comments on commit 7d3ba1e

Please sign in to comment.