Decorating the default DataAdapter

238 views
Skip to first unread message

Benjy Weinberger

unread,
Aug 2, 2015, 2:54:44 PM8/2/15
to select2
Hi, 

I'm new to select2 and want to change the MinimumInputLength behavior to remove the "Please enter x or more characters" prompt. 

I *think* the right way to do this is a custom decorator, something like:

function MyMinimumInputLength(decorated, $e, options) {
  this.minimumInputLength = options.get('minimumInputLength');
  decorated.call(this, $e, options);
}

MyMinimumInputLength.prototype.query = function(decorated, params, callback) {
  if ((params.term || '').length >= this.minimumInputLength) {
    decorated.call(this, params, callback);
  }
};

What I don't understand is, how do I get a reference to the default DataAdapter so I can add my decorator to it? The examples I've seen involve a custom DataAdapter, explicitly assigned to options.dataAdapter when the select2 is initialized. But that seems like overkill (plus I would have to manually apply all the other decorators I want, replicating existing logic). I want to do something like intercept the options after all defaults have been applied, take the value that Defaults.prototype.apply assigned to options.dataAdapter, and wrap it in my decorator. Is there a way to do that?

Thanks!

PS Even if there's a different way to remove that prompt, I'd like to understand the underlying question of how to decorate a non-custom adapter. Ta!

Benjy Weinberger

unread,
Aug 2, 2015, 3:39:31 PM8/2/15
to select2
Update: I got things working with this wrapper. It takes the options I was going to pass in to select2(), applies the default processing so that I get whatever dataAdapter my options implied, and then decorates it. But this seems pretty hacky.

function initSelect2ControlFromOptions(selector, options) {
  $.fn.select2.amd.require(
    ['select2/utils', 'select2/defaults'],
    function (Utils, Defaults) {
      options.dataAdapter = Utils.Decorate(Defaults.apply(options).dataAdapter, MyMinimumInputLength);
      $(selector).select2(options);
    }
  );
}

Is there a better way? Thanks!

Ali Shan Backlinks

unread,
Dec 20, 2025, 6:06:49 PM (2 days ago) Dec 20
to select2
  This is a thoughtful question and highlights a common challenge when working with Select2’s internal adapters. Understanding how to hook into or decorate the default DataAdapter without fully reimplementing it would definitely help developers customize behavior more cleanly, especially for UX tweaks like suppressing prompts https://nba2k20apk.com.ph/
Reply all
Reply to author
Forward
0 new messages