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

Commit

Permalink
refactor(buttons): use $render instead of model $watch
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Jun 15, 2013
1 parent 853199e commit 9c62409
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/buttons/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ angular.module('ui.bootstrap.buttons', [])
var value = scope.$eval(attrs.btnRadio);

//model -> UI
scope.$watch(function () {
return ngModelCtrl.$modelValue;
}, function (modelValue) {
if (angular.equals(modelValue, value)){
ngModelCtrl.$render = function () {
if (angular.equals(ngModelCtrl.$modelValue, value)){
element.addClass(activeClass);
} else {
element.removeClass(activeClass);
}
});
};

//ui->model
element.bind(toggleEvent, function () {
if (!element.hasClass(activeClass)) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(value);
ngModelCtrl.$render();
});
}
});
Expand All @@ -55,20 +54,19 @@ angular.module('ui.bootstrap.buttons', [])
falseValue = angular.isDefined(falseValue) ? falseValue : false;

//model -> UI
scope.$watch(function () {
return ngModelCtrl.$modelValue;
}, function (modelValue) {
if (angular.equals(modelValue, trueValue)) {
ngModelCtrl.$render = function () {
if (angular.equals(ngModelCtrl.$modelValue, trueValue)) {
element.addClass(activeClass);
} else {
element.removeClass(activeClass);
}
});
};

//ui->model
element.bind(toggleEvent, function () {
scope.$apply(function () {
ngModelCtrl.$setViewValue(element.hasClass(activeClass) ? falseValue : trueValue);
ngModelCtrl.$render();
});
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/buttons/test/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ describe('buttons', function () {

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

btn.click();
expect($scope.model).toEqual(false);
expect(btn).not.toHaveClass('active');
});

it('should toggle custom model values on click', function () {
Expand All @@ -54,8 +57,11 @@ describe('buttons', function () {

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

btn.click();
expect($scope.model).toEqual(0);
expect(btn).not.toHaveClass('active');
});
});

Expand Down Expand Up @@ -86,9 +92,13 @@ describe('buttons', function () {

btns.eq(0).click();
expect($scope.model).toEqual(1);
expect(btns.eq(0)).toHaveClass('active');
expect(btns.eq(1)).not.toHaveClass('active');

btns.eq(1).click();
expect($scope.model).toEqual(2);
expect(btns.eq(1)).toHaveClass('active');
expect(btns.eq(0)).not.toHaveClass('active');
});
});
});

0 comments on commit 9c62409

Please sign in to comment.