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

Commit

Permalink
feat(buttons): support dynamic values in btn-radio
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Jun 15, 2013
1 parent 9c62409 commit e8c5b54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/buttons/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ angular.module('ui.bootstrap.buttons', [])
require:'ngModel',
link:function (scope, element, attrs, ngModelCtrl) {

var value = scope.$eval(attrs.btnRadio);

//model -> UI
ngModelCtrl.$render = function () {
if (angular.equals(ngModelCtrl.$modelValue, value)){
if (angular.equals(ngModelCtrl.$modelValue, scope.$eval(attrs.btnRadio))){
element.addClass(activeClass);
} else {
element.removeClass(activeClass);
Expand All @@ -29,7 +27,7 @@ angular.module('ui.bootstrap.buttons', [])
element.bind(toggleEvent, function () {
if (!element.hasClass(activeClass)) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(value);
ngModelCtrl.$setViewValue(scope.$eval(attrs.btnRadio));
ngModelCtrl.$render();
});
}
Expand Down
19 changes: 19 additions & 0 deletions src/buttons/test/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,24 @@ describe('buttons', function () {
expect(btns.eq(1)).toHaveClass('active');
expect(btns.eq(0)).not.toHaveClass('active');
});

it('should watch btn-radio values and update state accordingly', function () {
$scope.values = ["value1", "value2"];

var btns = compileButtons('<button ng-model="model" btn-radio="values[0]">click1</button><button ng-model="model" btn-radio="values[1]">click2</button>', $scope);
expect(btns.eq(0)).not.toHaveClass('active');
expect(btns.eq(1)).not.toHaveClass('active');

$scope.model = "value2";
$scope.$digest();
expect(btns.eq(0)).not.toHaveClass('active');
expect(btns.eq(1)).toHaveClass('active');

$scope.values[1] = "value3";
$scope.model = "value3";
$scope.$digest();
expect(btns.eq(0)).not.toHaveClass('active');
expect(btns.eq(1)).toHaveClass('active');
});
});
});

0 comments on commit e8c5b54

Please sign in to comment.