From 9a96d828c4734f1195456827006615d1464efe40 Mon Sep 17 00:00:00 2001 From: Viktor Spatzig Date: Wed, 27 Jul 2016 12:17:56 +0200 Subject: [PATCH 1/3] Add option to refresh suggestion list on DOM only after async callback is done On some use cases it can be useful to render the search suggestions after all suggestions are available from synchronus and asynchronus callback of a dataset. So, the flag "updateOnAsync" is introduced to have the possibility to activate the new mode, to render the whole result after asynchronus callback only. If you want to display the whole result only, then this mode can be used now. Also, if you work with asynchronus data only and will not use a pending template, this mode ensures to prevent closing and reopen the dropdown menu on keypress (which can be misinterpreted as an ugly flickering, while the user types something) provokes by the empty or unused synchronus callback. --- src/typeahead/dataset.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/typeahead/dataset.js b/src/typeahead/dataset.js index 2894ee5f16..920e17d985 100644 --- a/src/typeahead/dataset.js +++ b/src/typeahead/dataset.js @@ -55,6 +55,8 @@ var Dataset = (function() { // a hint to figuring out of the source will return async suggestions this.async = _.isUndefined(o.async) ? this.source.length > 2 : !!o.async; + this.updateOnAsync = _.isUndefined(o.updateOnAsync) || !this.async ? false : o.updateOnAsync; + this._resetLastSuggestion(); this.$el = $(o.node) @@ -234,7 +236,7 @@ var Dataset = (function() { // ### public update: function update(query) { - var that = this, canceled = false, syncCalled = false, rendered = 0; + var that = this, canceled = false, syncCalled = false, rendered = 0, unrenderedSuggestions = []; // cancel possible pending update this.cancel(); @@ -253,9 +255,12 @@ var Dataset = (function() { syncCalled = true; suggestions = (suggestions || []).slice(0, that.limit); - rendered = suggestions.length; - that._overwrite(query, suggestions); + if (!that.updateOnAsync) { + rendered = suggestions.length; + that._overwrite(query, suggestions); + } else + unrenderedSuggestions = suggestions; if (rendered < that.limit && that.async) { that.trigger('asyncRequested', query); @@ -263,15 +268,19 @@ var Dataset = (function() { } function async(suggestions) { - suggestions = suggestions || []; + suggestions = unrenderedSuggestions.concat(suggestions).slice(0, that.limit - rendered); // if the update has been canceled or if the query has changed // do not render the suggestions as they've become outdated if (!canceled && rendered < that.limit) { that.cancel = $.noop; - var idx = Math.abs(rendered - that.limit); - rendered += idx; - that._append(query, suggestions.slice(0, idx)); + + if (!that.updateOnAsync) + that._append(query, suggestions); + else + that._overwrite(query, suggestions); + + rendered += suggestions.length; that.async && that.trigger("asyncReceived", query); } } From ae869f0b76bc2f94a24b2dbf52afb2dd49d75caa Mon Sep 17 00:00:00 2001 From: Viktor Spatzig Date: Mon, 15 Aug 2016 09:45:37 +0200 Subject: [PATCH 2/3] ESS-18475 - better initialisation of updateOnAsync variable --- src/typeahead/dataset.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typeahead/dataset.js b/src/typeahead/dataset.js index 920e17d985..4b162e61a1 100644 --- a/src/typeahead/dataset.js +++ b/src/typeahead/dataset.js @@ -55,7 +55,7 @@ var Dataset = (function() { // a hint to figuring out of the source will return async suggestions this.async = _.isUndefined(o.async) ? this.source.length > 2 : !!o.async; - this.updateOnAsync = _.isUndefined(o.updateOnAsync) || !this.async ? false : o.updateOnAsync; + this.updateOnAsync = this.async && o.updateOnAsync === true; this._resetLastSuggestion(); From 0b9cdb9b9a47e62559129868aa999dc3374aee03 Mon Sep 17 00:00:00 2001 From: Viktor Spatzig Date: Mon, 15 Aug 2016 09:56:21 +0200 Subject: [PATCH 3/3] ESS-18475 - fix pending- and not found template selection --- src/typeahead/dataset.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typeahead/dataset.js b/src/typeahead/dataset.js index 4b162e61a1..0c9351c23d 100644 --- a/src/typeahead/dataset.js +++ b/src/typeahead/dataset.js @@ -96,12 +96,12 @@ var Dataset = (function() { } // no suggestions, expecting async: overwrite dom with pending - else if (this.async && this.templates.pending) { + else if (this.async && !this.updateOnAsync && this.templates.pending) { this._renderPending(query); } // no suggestions, not expecting async: overwrite dom with not found - else if (!this.async && this.templates.notFound) { + else if ((!this.async || this.updateOnAsync) && this.templates.notFound) { this._renderNotFound(query); }