diff options
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;}); |
