Skip to content

Commit 0bda0f5

Browse files
authored
Merge pull request #1369 from kinow/monkey-patching-level-up
Modify counting for search limit in typeahead (patching it locally)
2 parents 34fa0db + c237f01 commit 0bda0f5

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

resource/js/docready.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,55 @@ $(function() { // DOCUMENT READY
742742
var typeaheadInstance = $typeahead.data("ttTypeahead");
743743
typeaheadInstance.menu.off("mouseenter.tt", ".tt-suggestion");
744744
typeaheadInstance.menu.off("mouseleave.tt", ".tt-suggestion");
745+
// Monkey-patching for:
746+
// - https://github.com/twitter/typeahead.js/pull/1774
747+
// - https://github.com/twitter/typeahead.js/blob/0700b186e98401127b051302365e34b09def2285/src/typeahead/dataset.js#L265-L278
748+
var update = function update(query) {
749+
var that = this, canceled = false, syncCalled = false, rendered = 0;
750+
751+
// cancel possible pending update
752+
this.cancel();
753+
754+
this.cancel = function cancel() {
755+
canceled = true;
756+
that.cancel = $.noop;
757+
that.async && that.trigger('asyncCanceled', query);
758+
};
759+
760+
this.source(query, sync, async);
761+
!syncCalled && sync([]);
762+
763+
function sync(suggestions) {
764+
if (syncCalled) { return; }
765+
766+
syncCalled = true;
767+
suggestions = (suggestions || []).slice(0, that.limit);
768+
rendered = suggestions.length;
769+
770+
that._overwrite(query, suggestions);
771+
772+
if (rendered < that.limit && that.async) {
773+
that.trigger('asyncRequested', query);
774+
}
775+
}
776+
777+
function async(suggestions) {
778+
suggestions = suggestions || [];
779+
780+
// if the update has been canceled or if the query has changed
781+
// do not render the suggestions as they've become outdated
782+
if (!canceled && rendered < that.limit) {
783+
that.cancel = $.noop;
784+
that._append(query, suggestions.slice(0, that.limit - rendered));
785+
786+
that.async && that.trigger('asyncReceived', query);
787+
rendered += suggestions.length;
788+
}
789+
}
790+
};
791+
typeaheadInstance.menu.datasets[0].update = update;
792+
update.bind(typeaheadInstance.menu.datasets[0]);
793+
// END
745794
}
746795

747796
// storing the search input before autocompletion changes it

0 commit comments

Comments
 (0)