Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to update list after async fetch is complete (avoid flickering) #1737

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/bloodhound.js
@@ -1,18 +1,18 @@
/*!
* typeahead.js 0.11.1
* https://github.com/twitter/typeahead.js
* Copyright 2013-2015 Twitter, Inc. and other contributors; Licensed MIT
* Copyright 2013-2018 Twitter, Inc. and other contributors; Licensed MIT
*/

(function(root, factory) {
if (typeof define === "function" && define.amd) {
define("bloodhound", [ "jquery" ], function(a0) {
return root["Bloodhound"] = factory(a0);
});
} else if (typeof exports === "object") {
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("jquery"));
} else {
root["Bloodhound"] = factory(jQuery);
root["Bloodhound"] = factory(root["jQuery"]);
}
})(this, function($) {
var _ = function() {
Expand Down
4 changes: 2 additions & 2 deletions dist/bloodhound.min.js

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions dist/typeahead.bundle.js
@@ -1,18 +1,18 @@
/*!
* typeahead.js 0.11.1
* https://github.com/twitter/typeahead.js
* Copyright 2013-2015 Twitter, Inc. and other contributors; Licensed MIT
* Copyright 2013-2018 Twitter, Inc. and other contributors; Licensed MIT
*/

(function(root, factory) {
if (typeof define === "function" && define.amd) {
define("bloodhound", [ "jquery" ], function(a0) {
return root["Bloodhound"] = factory(a0);
});
} else if (typeof exports === "object") {
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("jquery"));
} else {
root["Bloodhound"] = factory(jQuery);
root["Bloodhound"] = factory(root["jQuery"]);
}
})(this, function($) {
var _ = function() {
Expand Down Expand Up @@ -922,10 +922,10 @@
define("typeahead.js", [ "jquery" ], function(a0) {
return factory(a0);
});
} else if (typeof exports === "object") {
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("jquery"));
} else {
factory(jQuery);
factory(root["jQuery"]);
}
})(this, function($) {
var _ = function() {
Expand Down Expand Up @@ -1585,6 +1585,7 @@
this.templates = getTemplates(o.templates, this.displayFn);
this.source = o.source.__ttAdapter ? o.source.__ttAdapter() : o.source;
this.async = _.isUndefined(o.async) ? this.source.length > 2 : !!o.async;
this.updateOnAsync = this.async && o.updateOnAsync === true;
this._resetLastSuggestion();
this.$el = $(o.node).addClass(this.classes.dataset).addClass(this.classes.dataset + "-" + this.name);
}
Expand All @@ -1603,9 +1604,9 @@
suggestions = suggestions || [];
if (suggestions.length) {
this._renderSuggestions(query, suggestions);
} else if (this.async && this.templates.pending) {
} else if (this.async && !this.updateOnAsync && this.templates.pending) {
this._renderPending(query);
} else if (!this.async && this.templates.notFound) {
} else if ((!this.async || this.updateOnAsync) && this.templates.notFound) {
this._renderNotFound(query);
} else {
this._empty();
Expand Down Expand Up @@ -1695,7 +1696,7 @@
}, obj) : obj;
},
update: function update(query) {
var that = this, canceled = false, syncCalled = false, rendered = 0;
var that = this, canceled = false, syncCalled = false, rendered = 0, unrenderedSuggestions = [];
this.cancel();
this.cancel = function cancel() {
canceled = true;
Expand All @@ -1710,18 +1711,26 @@
}
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);
}
}
function async(suggestions) {
suggestions = suggestions || [];
suggestions = unrenderedSuggestions.concat(suggestions).slice(0, that.limit - rendered);
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
if (!that.updateOnAsync) {
that._append(query, suggestions);
} else {
that._overwrite(query, suggestions);
}
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));
that.async && that.trigger("asyncReceived", query);
}
}
Expand Down
6 changes: 3 additions & 3 deletions dist/typeahead.bundle.min.js

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions dist/typeahead.jquery.js
@@ -1,18 +1,18 @@
/*!
* typeahead.js 0.11.1
* https://github.com/twitter/typeahead.js
* Copyright 2013-2015 Twitter, Inc. and other contributors; Licensed MIT
* Copyright 2013-2018 Twitter, Inc. and other contributors; Licensed MIT
*/

(function(root, factory) {
if (typeof define === "function" && define.amd) {
define("typeahead.js", [ "jquery" ], function(a0) {
return factory(a0);
});
} else if (typeof exports === "object") {
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("jquery"));
} else {
factory(jQuery);
factory(root["jQuery"]);
}
})(this, function($) {
var _ = function() {
Expand Down Expand Up @@ -672,6 +672,7 @@
this.templates = getTemplates(o.templates, this.displayFn);
this.source = o.source.__ttAdapter ? o.source.__ttAdapter() : o.source;
this.async = _.isUndefined(o.async) ? this.source.length > 2 : !!o.async;
this.updateOnAsync = this.async && o.updateOnAsync === true;
this._resetLastSuggestion();
this.$el = $(o.node).addClass(this.classes.dataset).addClass(this.classes.dataset + "-" + this.name);
}
Expand All @@ -690,9 +691,9 @@
suggestions = suggestions || [];
if (suggestions.length) {
this._renderSuggestions(query, suggestions);
} else if (this.async && this.templates.pending) {
} else if (this.async && !this.updateOnAsync && this.templates.pending) {
this._renderPending(query);
} else if (!this.async && this.templates.notFound) {
} else if ((!this.async || this.updateOnAsync) && this.templates.notFound) {
this._renderNotFound(query);
} else {
this._empty();
Expand Down Expand Up @@ -782,7 +783,7 @@
}, obj) : obj;
},
update: function update(query) {
var that = this, canceled = false, syncCalled = false, rendered = 0;
var that = this, canceled = false, syncCalled = false, rendered = 0, unrenderedSuggestions = [];
this.cancel();
this.cancel = function cancel() {
canceled = true;
Expand All @@ -797,18 +798,26 @@
}
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);
}
}
function async(suggestions) {
suggestions = suggestions || [];
suggestions = unrenderedSuggestions.concat(suggestions).slice(0, that.limit - rendered);
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
if (!that.updateOnAsync) {
that._append(query, suggestions);
} else {
that._overwrite(query, suggestions);
}
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));
that.async && that.trigger("asyncReceived", query);
}
}
Expand Down
4 changes: 2 additions & 2 deletions dist/typeahead.jquery.min.js

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions src/typeahead/dataset.js
Expand Up @@ -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 = this.async && o.updateOnAsync === true;

this._resetLastSuggestion();

this.$el = $(o.node)
Expand Down Expand Up @@ -94,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);
}

Expand Down Expand Up @@ -234,8 +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();

Expand All @@ -253,25 +254,33 @@ 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);
}
}

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;
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));
if (!that.updateOnAsync) {
that._append(query, suggestions);
}
else {
that._overwrite(query, suggestions);
}

rendered += suggestions.length;
that.async && that.trigger('asyncReceived', query);
}
}
Expand Down