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

Commit

Permalink
fix(ngModelOptions): enable overriding the default with a debounce of…
Browse files Browse the repository at this point in the history
… 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 #7205
  • Loading branch information
joelhooks authored and petebacondarwin committed May 4, 2014
1 parent 8c08fcf commit c56e32a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1822,9 +1822,20 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* @param {string} trigger Event that triggered the update.
*/
this.$setViewValue = function(value, trigger) {
var debounceDelay = ctrl.$options && (isObject(ctrl.$options.debounce)
? (ctrl.$options.debounce[trigger] || ctrl.$options.debounce['default'] || 0)
: ctrl.$options.debounce) || 0;
var debounceDelay = 0,
options = ctrl.$options,
debounce;

if(options && isDefined(options.debounce)) {
debounce = options.debounce;
if(isNumber(debounce)) {
debounceDelay = debounce;
} else if(isNumber(debounce[trigger])) {
debounceDelay = debounce[trigger];
} else if (isNumber(debounce['default'])) {
debounceDelay = debounce['default'];
}
}

$timeout.cancel(pendingDebounce);
if (debounceDelay) {
Expand Down
19 changes: 19 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,25 @@ describe('input', function() {
expect(scope.checkbox).toBe(false);
}));

it('should allow selecting 0 for non-default debounce timeouts for each event on checkboxes', inject(function($timeout) {
compileInput('<input type="checkbox" ng-model="checkbox" '+
'ng-model-options="{ '+
'updateOn: \'default blur\', debounce: { default: 10000, blur: 0 } }"'+
'/>');

inputElm[0].checked = false;
browserTrigger(inputElm, 'click');
expect(scope.checkbox).toBe(undefined);
$timeout.flush(8000);
expect(scope.checkbox).toBe(undefined);
$timeout.flush(3000);
expect(scope.checkbox).toBe(true);
inputElm[0].checked = true;
browserTrigger(inputElm, 'click');
browserTrigger(inputElm, 'blur');
$timeout.flush(0);
expect(scope.checkbox).toBe(false);
}));

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

0 comments on commit c56e32a

Please sign in to comment.