Skip to content

Instantly share code, notes, and snippets.

@govorov
Last active December 6, 2023 23:58
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save govorov/3ee75f54170735153349b0a430581195 to your computer and use it in GitHub Desktop.
Save govorov/3ee75f54170735153349b0a430581195 to your computer and use it in GitHub Desktop.
$.fn.select2.amd.define('select2/data/extended-ajax',['./ajax','../utils','jquery'], function(AjaxAdapter, Utils, $){
function ExtendedAjaxAdapter ($element,options) {
//we need explicitly process minimumInputLength value
//to decide should we use AjaxAdapter or return defaultResults,
//so it is impossible to use MinimumLength decorator here
this.minimumInputLength = options.get('minimumInputLength');
this.defaultResults = options.get('defaultResults');
ExtendedAjaxAdapter.__super__.constructor.call(this,$element,options);
}
Utils.Extend(ExtendedAjaxAdapter,AjaxAdapter);
//override original query function to support default results
var originQuery = AjaxAdapter.prototype.query;
ExtendedAjaxAdapter.prototype.query = function (params, callback) {
var defaultResults = (typeof this.defaultResults == 'function') ? this.defaultResults.call(this) : this.defaultResults;
if (defaultResults && defaultResults.length && (!params.term || params.term.length < this.minimumInputLength)){
var processedResults = this.processResults(defaultResults,params.term);
callback(processedResults);
}
else {
originQuery.call(this, params, callback);
}
};
return ExtendedAjaxAdapter;
});
@destan
Copy link

destan commented May 8, 2020

For anyone coming to this with the same issue I had:

@vitorspencer thanks for the solution, working good 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment