1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Merge pull request #3094 from mabashian/2935-backspace-inv-cred-jt

awlookup timeout instead of GET with every keystroke
This commit is contained in:
Michael Abashian 2016-07-22 15:11:45 -04:00 committed by GitHub
commit 6edc3abce2

View File

@ -390,46 +390,57 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper'])
// lookup Validate lookup value against API // lookup Validate lookup value against API
// //
.directive('awlookup', ['Rest', function(Rest) { .directive('awlookup', ['Rest', '$timeout', function(Rest, $timeout) {
return { return {
require: 'ngModel', require: 'ngModel',
link: function(scope, elm, attrs, ctrl) { link: function(scope, elm, attrs, ctrl) {
var restTimeout;
ctrl.$parsers.unshift( function(viewValue) { ctrl.$parsers.unshift( function(viewValue) {
if (viewValue !== '' && viewValue !== null) { if (viewValue !== '' && viewValue !== null) {
var url = elm.attr('data-url'); var url = elm.attr('data-url');
url = url.replace(/\:value/, encodeURI(viewValue)); url = url.replace(/\:value/, encodeURI(viewValue));
scope[elm.attr('data-source')] = null; scope[elm.attr('data-source')] = null;
Rest.setUrl(url); if(restTimeout) {
Rest.get().then( function(data) { $timeout.cancel(restTimeout);
var results = data.data.results; }
if (results.length > 0) { restTimeout = $timeout( function(){
scope[elm.attr('data-source')] = results[0].id; Rest.setUrl(url);
Rest.get().then( function(data) {
var results = data.data.results;
if (results.length > 0) {
scope[elm.attr('data-source')] = results[0].id;
// For user lookups the API endpoint doesn't // For user lookups the API endpoint doesn't
// have a `name` property, so this is `undefined` // have a `name` property, so this is `undefined`
// which causes the input to clear after typing // which causes the input to clear after typing
// a valid value O_o // a valid value O_o
// //
// Only assign if there is a value, so that we avoid // Only assign if there is a value, so that we avoid
// this situation. // this situation.
// //
// TODO: Evaluate if assigning name on the scope is // TODO: Evaluate if assigning name on the scope is
// even necessary at all. // even necessary at all.
// //
if (!_.isEmpty(results[0].name)) { if (!_.isEmpty(results[0].name)) {
scope[elm.attr('name')] = results[0].name; scope[elm.attr('name')] = results[0].name;
}
ctrl.$setValidity('required', true);
ctrl.$setValidity('awlookup', true);
return viewValue;
} }
ctrl.$setValidity('required', true); ctrl.$setValidity('required', true);
ctrl.$setValidity('awlookup', true); ctrl.$setValidity('awlookup', false);
return viewValue; return undefined;
} });
ctrl.$setValidity('required', true); }, 750);
ctrl.$setValidity('awlookup', false);
return undefined;
});
} }
else { else {
if(restTimeout) {
$timeout.cancel(restTimeout);
}
ctrl.$setValidity('awlookup', true); ctrl.$setValidity('awlookup', true);
scope[elm.attr('data-source')] = null; scope[elm.attr('data-source')] = null;
} }