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

Commit 157f614

Browse files
bekospkozlowski-opensource
authored andcommittedSep 19, 2013
fix(accordion): assign is-open to correct scope
Closes #1034
1 parent 006986d commit 157f614

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed
 

‎src/accordion/accordion.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
55
})
66

77
.controller('AccordionController', ['$scope', '$attrs', 'accordionConfig', function ($scope, $attrs, accordionConfig) {
8-
8+
99
// This array keeps track of the accordion groups
1010
this.groups = [];
1111

12+
// Keep reference to user's scope to properly assign `is-open`
13+
this.scope = $scope;
14+
1215
// Ensure that all the groups in this accordion are closed, unless close-others explicitly says not to
1316
this.closeOthers = function(openGroup) {
1417
var closeOthers = angular.isDefined($attrs.closeOthers) ? $scope.$eval($attrs.closeOthers) : accordionConfig.closeOthers;
@@ -78,20 +81,17 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
7881
getIsOpen = $parse(attrs.isOpen);
7982
setIsOpen = getIsOpen.assign;
8083

81-
scope.$watch(
82-
function watchIsOpen() { return getIsOpen(scope.$parent); },
83-
function updateOpen(value) { scope.isOpen = value; }
84-
);
85-
86-
scope.isOpen = getIsOpen ? getIsOpen(scope.$parent) : false;
84+
accordionCtrl.scope.$watch(getIsOpen, function(value) {
85+
scope.isOpen = !!value;
86+
});
8787
}
8888

8989
scope.$watch('isOpen', function(value) {
9090
if ( value ) {
9191
accordionCtrl.closeOthers(scope);
9292
}
9393
if ( setIsOpen ) {
94-
setIsOpen(scope.$parent, value);
94+
setIsOpen(accordionCtrl.scope, value);
9595
}
9696
});
9797
}

‎src/accordion/docs/demo.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</label>
66

77
<accordion close-others="oneAtATime">
8-
<accordion-group heading="Static Header">
8+
<accordion-group heading="Static Header, initially expanded" is-open="true">
99
This content is straight in the template.
1010
</accordion-group>
1111
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
@@ -16,9 +16,9 @@
1616
<button class="btn btn-small" ng-click="addItem()">Add Item</button>
1717
<div ng-repeat="item in items">{{item}}</div>
1818
</accordion-group>
19-
<accordion-group>
19+
<accordion-group is-open="isopen">
2020
<accordion-heading>
21-
I can have markup, too! <i class="icon-check"></i>
21+
I can have markup, too! <i class="pull-right" ng-class="{'icon-chevron-down': isopen, 'icon-chevron-right': !isopen}"></i>
2222
</accordion-heading>
2323
This is just some content to illustrate fancy headings.
2424
</accordion-group>

‎src/accordion/test/accordionSpec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,17 @@ describe('accordion', function () {
232232
it('should open the group with isOpen set to true', function () {
233233
expect(findGroupBody(0).scope().isOpen).toBe(false);
234234
expect(findGroupBody(1).scope().isOpen).toBe(true);
235-
});
235+
});
236+
237+
it('should toggle variable on element click', function() {
238+
findGroupLink(0).click();
239+
scope.$digest();
240+
expect(scope.open1).toBe(true);
241+
242+
findGroupLink(0).click();
243+
scope.$digest();
244+
expect(scope.open1).toBe(false);
245+
});
236246
});
237247

238248
describe('is-open attribute with dynamic content', function() {

0 commit comments

Comments
 (0)