Yanick Rochon
Yanick Rochon

Reputation: 53521

RequireJS module dependencies and includes

In my RequestJS config file, I am loading select2 and I'd like to also load locales for it. I currently have this setup :

require.config({
  enforceDefine: true,
  baseUrl: '/js',
  shim: {
    '[email protected]': {
      deps: [ 'jquery' ],
      exports: '$'        // export jQuery...
    },
    '[email protected]': {
      exports: '$'
    },
    'select2': {
      deps: [ 
        'bootstrap',
        'lib/select2_locales/select2_locale_fr',
        /// NOTE : add locales here
        'css!/css/select2/select2.css',
        'css!/css/select2/select2-bootstrap.css',
      ],
      exports: '$.fn.select2'
    }
  },
  paths: {
    '[email protected]': 'lib/bootstrap.min',
    '[email protected]': 'lib/jquery.min'
  },
  map: {
    '*': {
      'css': '[email protected]',
      'jquery': '[email protected]',
      'bootstrap': '[email protected]'
    }
  }
});

The problem, here, is that lib/select2_locales/select2_locale_fr needs to be loaded after select2.min.js.

Is there some way I can change this configuration, or add an option, so I can load the locales along with select2, from the require config file?

Upvotes: 1

Views: 384

Answers (1)

lostinplace
lostinplace

Reputation: 1518

If that is the case, then select2_locale_fr is not a dependency of select 2, select2 is a dependency of select2_locale_fr

define('locale_specific_select2',['select2'], function(select2){
  select2.accomodateNewLocale= function(stuff){
    doWhatYouNeedToWith(stuff);
  }
  locale = determineLocale()
  function processLocaleData(localeData){
    select2.accomodateNewLocale(localeData);
  }
  require(['pathTOLocalePlugin'+locale], processLocaleData);
}

Upvotes: 1

Related Questions