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 true / false values in btn-checkbox
Browse files Browse the repository at this point in the history
Closes #666
  • Loading branch information
pkozlowski-opensource committed Jul 16, 2013
1 parent 63ae7e1 commit 3e30cd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/buttons/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ angular.module('ui.bootstrap.buttons', [])
require:'ngModel',
link:function (scope, element, attrs, ngModelCtrl) {

var trueValue = scope.$eval(attrs.btnCheckboxTrue);
var falseValue = scope.$eval(attrs.btnCheckboxFalse);
function getTrueValue() {
var trueValue = scope.$eval(attrs.btnCheckboxTrue);
return angular.isDefined(trueValue) ? trueValue : true;
}

trueValue = angular.isDefined(trueValue) ? trueValue : true;
falseValue = angular.isDefined(falseValue) ? falseValue : false;
function getFalseValue() {
var falseValue = scope.$eval(attrs.btnCheckboxFalse);
return angular.isDefined(falseValue) ? falseValue : false;
}

//model -> UI
ngModelCtrl.$render = function () {
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, trueValue));
element.toggleClass(activeClass, angular.equals(ngModelCtrl.$modelValue, getTrueValue()));
};

//ui->model
element.bind(toggleEvent, function () {
scope.$apply(function () {
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? falseValue : trueValue);
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? getFalseValue() : getTrueValue());
ngModelCtrl.$render();
});
});
Expand Down
17 changes: 17 additions & 0 deletions src/buttons/test/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ describe('buttons', function () {
expect($scope.model).toEqual(0);
expect(btn).not.toHaveClass('active');
});

it('should monitor true / false value changes - issue 666', function () {

$scope.model = 1;
$scope.trueVal = 1;
var btn = compileButton('<button ng-model="model" btn-checkbox btn-checkbox-true="trueVal">click</button>', $scope);

expect(btn).toHaveClass('active');
expect($scope.model).toEqual(1);

$scope.model = 2;
$scope.trueVal = 2;
$scope.$digest();

expect(btn).toHaveClass('active');
expect($scope.model).toEqual(2);
});
});

describe('radio', function () {
Expand Down

0 comments on commit 3e30cd9

Please sign in to comment.