From aad380028fb1eec0defbeced39926f4626e02855 Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Tue, 4 Aug 2015 22:46:39 +0300 Subject: [PATCH] bitbake: toastergui: libtoaster: typeahead resiliency for slow server When we have a slow request we don't want to see the typeahead results changing in the middle of typing the item. To do this we make sure that requests to the typeahead can only happen sequentially and that if results have been retrieved but the input is now empty we don't bother showing the results. (Bitbake rev: 0b508e8b4a9e25c223c6c11ecd6d0e1aab727e8c) Signed-off-by: Michael Wood Signed-off-by: Ed Bartosh Signed-off-by: Richard Purdie --- .../toaster/toastergui/static/js/libtoaster.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/bitbake/lib/toaster/toastergui/static/js/libtoaster.js b/bitbake/lib/toaster/toastergui/static/js/libtoaster.js index 587f51fff9..34a3fbb1fb 100644 --- a/bitbake/lib/toaster/toastergui/static/js/libtoaster.js +++ b/bitbake/lib/toaster/toastergui/static/js/libtoaster.js @@ -18,15 +18,24 @@ var libtoaster = (function (){ if (!xhrUrl || xhrUrl.length === 0) throw("No url to typeahead supplied"); + var xhrReq; + jQElement.typeahead({ source: function(query, process){ xhrParams.search = query; - $.getJSON(xhrUrl, this.options.xhrParams, function(data){ + + /* If we have a request in progress don't fire off another one*/ + if (xhrReq) + xhrReq.abort(); + + xhrReq = $.getJSON(xhrUrl, this.options.xhrParams, function(data){ if (data.error !== "ok") { console.log("Error getting data from server "+data.error); return; } + xhrReq = null; + return process(data.results); }); }, @@ -41,6 +50,9 @@ var libtoaster = (function (){ return 0; } + if (this.$element.val().length === 0) + return 0; + return 1; }, highlighter: function (item) { @@ -52,6 +64,7 @@ var libtoaster = (function (){ sorter: function (items) { return items; }, xhrUrl: xhrUrl, xhrParams: xhrParams, + xhrReq: xhrReq, }); @@ -528,6 +541,9 @@ $(document).ready(function() { }); $(document).ajaxError(function(event, jqxhr, settings, errMsg){ + if (errMsg === 'abort') + return; + console.warn("Problem with xhr call"); console.warn(errMsg); console.warn(jqxhr.responseText);