diff options
| author | Misko Hevery | 2010-04-16 14:01:29 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-04-16 14:01:29 -0700 |
| commit | deb86fe357a901889bc4289087f0b9e69cb8a302 (patch) | |
| tree | fce4db8501a6c24430d611c95a4aa001119c7b89 /src/validators.js | |
| parent | 70e401ef100614295fc808e32f0142f07c315461 (diff) | |
| download | angular.js-deb86fe357a901889bc4289087f0b9e69cb8a302.tar.bz2 | |
lots of small fixes
Diffstat (limited to 'src/validators.js')
| -rw-r--r-- | src/validators.js | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/src/validators.js b/src/validators.js index 4544b96c..27b4b404 100644 --- a/src/validators.js +++ b/src/validators.js @@ -81,33 +81,52 @@ foreach({ } }, - 'asynchronous': function(text, asynchronousFn) { - var element = this['$element']; - var cache = element.data('$validateState'); + /* + * cache is attached to the element + * cache: { + * inputs : { + * 'user input': { + * response: server response, + * error: validation error + * }, + * current: 'current input' + * } + * + */ + 'asynchronous': function(input, asynchronousFn, updateFn) { + if (!input) return; + var scope = this; + var element = scope.$element; + var cache = element.data('$asyncValidator'); if (!cache) { - cache = { state: {}}; - element.data('$validateState', cache); + element.data('$asyncValidator', cache = {inputs:{}}); } - var state = cache.state[text]; - cache.lastKey = text; - if (state === undefined) { - // we have never seen this before, Request it + + cache.current = input; + + var inputState = cache.inputs[input]; + if (!inputState) { + cache.inputs[input] = inputState = { inFlight: true }; + scope.$invalidWidgets.markInvalid(scope.$element); element.addClass('ng-input-indicator-wait'); - state = cache.state[text] = null; - (asynchronousFn || noop)(text, function(error){ - state = cache.state[text] = error ? error : false; - if (cache.state[cache.lastKey] !== null) { + asynchronousFn(input, function(error, data) { + inputState.response = data; + inputState.error = error; + inputState.inFlight = false; + if (cache.current == input) { element.removeClass('ng-input-indicator-wait'); + scope.$invalidWidgets.markValid(element); } - elementError(element, NG_VALIDATION_ERROR, error); + element.data('$validate')(input); + scope.$root.$eval(); }); - } - - if (state === null && this['$invalidWidgets']){ + } else if (inputState.inFlight) { // request in flight, mark widget invalid, but don't show it to user - this['$invalidWidgets'].markInvalid(this.$element); + scope.$invalidWidgets.markInvalid(scope.$element); + } else { + (updateFn||noop)(inputState.response); } - return state; + return inputState.error; } }, function(v,k) {angularValidator[k] = v;}); |
