Created
February 19, 2015 16:39
-
-
Save timkelty/ae79d180acc9d3054e02 to your computer and use it in GitHub Desktop.
Custom select2 adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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