Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/typeahead/dataset.js
Original file line number Diff line number Diff line change
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,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();
Expand All @@ -253,25 +255,32 @@ 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;
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);
}
}
Expand Down