Skip to content

Commit c56e32a

Browse files
joelhookspetebacondarwin
authored andcommitted
fix(ngModelOptions): enable overriding the default with a debounce of zero
Because of how the logic was set up, a value of `0` was assumed to be the same as `undefined`, which meant that you couldn't override the default debounce delay with a value of zero. For example, the following assigned a debounce delay of 500ms to the `blur` event. ``` ngModelOptions="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" ``` Closes angular#7205
1 parent 8c08fcf commit c56e32a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/ng/directive/input.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1822,9 +1822,20 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18221822
* @param {string} trigger Event that triggered the update.
18231823
*/
18241824
this.$setViewValue = function(value, trigger) {
1825-
var debounceDelay = ctrl.$options && (isObject(ctrl.$options.debounce)
1826-
? (ctrl.$options.debounce[trigger] || ctrl.$options.debounce['default'] || 0)
1827-
: ctrl.$options.debounce) || 0;
1825+
var debounceDelay = 0,
1826+
options = ctrl.$options,
1827+
debounce;
1828+
1829+
if(options && isDefined(options.debounce)) {
1830+
debounce = options.debounce;
1831+
if(isNumber(debounce)) {
1832+
debounceDelay = debounce;
1833+
} else if(isNumber(debounce[trigger])) {
1834+
debounceDelay = debounce[trigger];
1835+
} else if (isNumber(debounce['default'])) {
1836+
debounceDelay = debounce['default'];
1837+
}
1838+
}
18281839

18291840
$timeout.cancel(pendingDebounce);
18301841
if (debounceDelay) {

test/ng/directive/inputSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,25 @@ describe('input', function() {
858858
expect(scope.checkbox).toBe(false);
859859
}));
860860

861+
it('should allow selecting 0 for non-default debounce timeouts for each event on checkboxes', inject(function($timeout) {
862+
compileInput('<input type="checkbox" ng-model="checkbox" '+
863+
'ng-model-options="{ '+
864+
'updateOn: \'default blur\', debounce: { default: 10000, blur: 0 } }"'+
865+
'/>');
866+
867+
inputElm[0].checked = false;
868+
browserTrigger(inputElm, 'click');
869+
expect(scope.checkbox).toBe(undefined);
870+
$timeout.flush(8000);
871+
expect(scope.checkbox).toBe(undefined);
872+
$timeout.flush(3000);
873+
expect(scope.checkbox).toBe(true);
874+
inputElm[0].checked = true;
875+
browserTrigger(inputElm, 'click');
876+
browserTrigger(inputElm, 'blur');
877+
$timeout.flush(0);
878+
expect(scope.checkbox).toBe(false);
879+
}));
861880

862881
it('should inherit model update settings from ancestor elements', inject(function($timeout) {
863882
var doc = $compile(

0 commit comments

Comments
 (0)