From cb9aabfe6da306c8b7899138999d9bf28eab8e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Ram=C3=B3n=20L=C3=B3pez?= Date: Sat, 5 Apr 2014 21:44:51 +0200 Subject: [PATCH] fix(ng-model-options): Canceling debounces now forces a view reset --- src/ng/directive/input.js | 21 +++++++++++++++++++-- test/ng/directive/inputSpec.js | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 206bc82ea294..d881d609b89f 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1701,10 +1701,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ * This method should be called before directly update a debounced model from the scope in * order to prevent unintended future changes of the model value because of a delayed event. */ - this.$cancelDebounce = function() { + this.$cancelDebounce = function(skipRender) { if ( pendingDebounce ) { $timeout.cancel(pendingDebounce); pendingDebounce = null; + if ( !skipRender ) { + this.$resetModelValue(); + } } }; @@ -1769,7 +1772,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ ? (this.$options.debounce[trigger] || this.$options.debounce['default'] || 0) : this.$options.debounce) || 0; - that.$cancelDebounce(); + that.$cancelDebounce(true); if ( debounceDelay ) { pendingDebounce = $timeout(function() { pendingDebounce = null; @@ -1783,6 +1786,20 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ // model -> value var ctrl = this; + this.$resetModelValue = function() { + var formatters = ctrl.$formatters, + idx = formatters.length; + + var value = ngModelGet($scope); + ctrl.$modelValue = value; + while(idx--) { + value = formatters[idx](value); + } + + ctrl.$viewValue = value; + ctrl.$render(); + }; + $scope.$watch(function ngModelWatch() { var value = ngModelGet($scope); diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 5046d4788fe5..9f9ae3d66f82 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -863,6 +863,21 @@ describe('input', function() { expect(scope.name).toEqual(undefined); })); + + it("should reset the input value when cancelDebounce is called", inject(function($timeout) { + compileInput( + '
'+ + ''+ + '
'); + + inputElm.val('a'); + scope.test.alias.$cancelDebounce(); + expect(inputElm.val()).toBe(''); + $timeout.flush(3000); + expect(inputElm.val()).toBe(''); + })); + }); it('should allow complex reference binding', function() {