Skip to content

Instantly share code, notes, and snippets.

@timkelty
Created February 19, 2015 16:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkelty/ae79d180acc9d3054e02 to your computer and use it in GitHub Desktop.
Save timkelty/ae79d180acc9d3054e02 to your computer and use it in GitHub Desktop.
Custom select2 adapter
// jshint maxparams: 8
define([
'jquery',
'fmjs/amd/src/fm.timer',
'Select2/src/js/select2/utils',
'Select2/src/js/select2/dropdown',
'Select2/src/js/select2/dropdown/attachContainer',
'Select2/src/js/select2/dropdown/search',
'Select2/src/js/select2/dropdown/minimumResultsForSearch',
'Select2/src/js/jquery.select2',
], function($, FM, Utils, DropdownAdapter, AttachContainer, DropdownSearch, MinimumResultsForSearch) {
var $selects = $('select');
var fm = new FM();
var dropdownAdapter = Utils.Decorate(
Utils.Decorate(
DropdownAdapter,
DropdownSearch
),
AttachContainer
);
dropdownAdapter = Utils.Decorate(dropdownAdapter, MinimumResultsForSearch);
var opts = {
theme: 'custom',
minimumResultsForSearch: 15,
dropdownAdapter: dropdownAdapter,
};
$selects.each(function(index, el) {
var $this = $(this);
$this.select2($.extend(opts, {
placeholder: $this.attr('placeholder')
}));
});
// Seems like we shouldn't have to do this ourselves,
// but width: 'resolve' doesn't seem to work on resize
$(window).on('resize', fm.debounce(function() {
var data = $selects.data('select2');
if (data && data.$element) {
var width = data.$element.width();
$selects.select2($.extend(opts, { width: width }));
}
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment