From e8c5b548c732e1c3f87c944e41f9330733979364 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sat, 15 Jun 2013 22:31:03 +0200 Subject: [PATCH] feat(buttons): support dynamic values in btn-radio --- src/buttons/buttons.js | 6 ++---- src/buttons/test/buttons.spec.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/buttons/buttons.js b/src/buttons/buttons.js index 41ea0a5d9b..39cf42fccc 100644 --- a/src/buttons/buttons.js +++ b/src/buttons/buttons.js @@ -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); @@ -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(); }); } diff --git a/src/buttons/test/buttons.spec.js b/src/buttons/test/buttons.spec.js index 6c0fa65aec..b7ee29d26c 100644 --- a/src/buttons/test/buttons.spec.js +++ b/src/buttons/test/buttons.spec.js @@ -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('', $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'); + }); }); }); \ No newline at end of file