I have a RequireJS setup that loads the Select2 jQuery plugin.
I have an issue however that means on page load, the browser default select input is shown for a few seconds, and then swapped out for the advanced Select2 input presumably as RequireJS is loading my scripts asynchronously.
Is there a way I can avoid this without having to include the script in the HTML?
This is my RequireJS setup:
require.config({
baseUrl: '/assets/js',
paths: {
jquery: '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
async: 'libs/async',
modernizr: 'libs/modernizr-custom',
jqueryUI: 'libs/jquery-ui.min',
bootstrap: 'libs/bootstrap.min',
select2: 'libs/select2.min',
},
shim: {
'select2': {
deps: ['jquery'],
exports: 'Select2'
},
'bootstrap': {
deps: ['jquery'],
exports: 'Bootstrap'
},
'jqueryUI': {
deps: ['jquery'],
exports: 'jQueryUI'
},
}
});
requirejs(["app/polyfills"]);
requirejs(["app/filter"]);
requirejs(["app/select"]); // Select2 plugin trigger
requirejs(["app/datepicker"]);
requirejs(["app/popover"]);
requirejs(["app/dropdown"]);
requirejs(["app/tooltip"]);
requirejs(["app/layout"]);
requirejs(["app/menu"]);
requirejs(["app/toggles"]);
And this is the actual script:
define(['select2'], function(Select2) {
var app = {};
app.select = (function(){
var module = {};
module.init = function(){
$(".select-advanced").select2({
width: 'off',
dropdownAutoWidth: 'true',
});
};
return module;
})();
$(document).ready(function(){
app.select.init();
console.log('select');
});
});