Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(input): constant exprs for ngTrueValue/ngFalseValue #5346

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,9 @@ function radioInputType(scope, element, attr, ctrl) {
attr.$observe('value', ctrl.$render);
}

function checkboxInputType(scope, element, attr, ctrl) {
var trueValue = attr.ngTrueValue,
falseValue = attr.ngFalseValue;

if (!isString(trueValue)) trueValue = true;
if (!isString(falseValue)) falseValue = false;
function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $parse) {
var trueValue = getCheckedValue(attr.ngTrueValue, true),
falseValue = getCheckedValue(attr.ngFalseValue, false);

element.on('click', function() {
scope.$apply(function() {
Expand All @@ -676,16 +673,31 @@ function checkboxInputType(scope, element, attr, ctrl) {

// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
ctrl.$isEmpty = function(value) {
return value !== trueValue;
return !equals(value, trueValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a perf issue if the value is a large object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree... but I think that's unlikely to be the case for this sort of object. === prevents the use of object literals, which might not be good

};

ctrl.$formatters.push(function(value) {
return value === trueValue;
return equals(value, trueValue);
});

ctrl.$parsers.push(function(value) {
return value ? trueValue : falseValue;
});

function getCheckedValue(expr, defaultValue) {
if (isString(expr)) {
var getter = $parse(expr),
value = getter(scope);
// If the resulting value is undefined, and not the constant `undefined`, then
// return the original string expression, it was probably intended as a string
// literal.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider just breaking this api to avoid confusing behavior since now ngTrueValue is sometimes a string and sometimes an expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, well it's a simple thing to make it always an expression, if that's the way to go

if (isUndefined(value) && !getter.constant) {
return expr;
}
return value;
}
return defaultValue;
}
}


Expand Down Expand Up @@ -812,14 +824,14 @@ function checkboxInputType(scope, element, attr, ctrl) {
</doc:scenario>
</doc:example>
*/
var inputDirective = ['$browser', '$sniffer', function($browser, $sniffer) {
var inputDirective = ['$browser', '$sniffer', '$parse', function($browser, $sniffer, $parse) {
return {
restrict: 'E',
require: '?ngModel',
link: function(scope, element, attr, ctrl) {
if (ctrl) {
(inputType[lowercase(attr.type)] || inputType.text)(scope, element, attr, ctrl, $sniffer,
$browser);
$browser, $parse);
}
}
};
Expand Down
42 changes: 42 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,48 @@ describe('input', function() {
});


it('should parse numeric ng-true-value/ng-false-value', function() {
compileInput('<input type="checkbox" ng-model="value" ' +
'ng-true-value="12345" ng-false-value="0">');
scope.$apply(function() {
scope.value = 12345;
});
expect(inputElm[0].checked).toBe(true);

scope.$apply(function() {
scope.value = 0;
});
expect(inputElm[0].checked).toBe(false);

browserTrigger(inputElm, 'click');
expect(scope.value).toBe(12345);

browserTrigger(inputElm, 'click');
expect(scope.value).toBe(0);
});


it('should parse object ng-true-value/ng-false-value', function() {
compileInput('<input type="checkbox" ng-model="value" ' +
'ng-true-value="{num: 12345}" ng-false-value="null">');
scope.$apply(function() {
scope.value = {num: 12345};
});
expect(inputElm[0].checked).toBe(true);

scope.$apply(function() {
scope.value = null;
});
expect(inputElm[0].checked).toBe(false);

browserTrigger(inputElm, 'click');
expect(scope.value.num).toBe(12345);

browserTrigger(inputElm, 'click');
expect(scope.value).toBe(null);
});


it('should be required if false', function() {
compileInput('<input type="checkbox" ng:model="value" required />');

Expand Down