Requiring additional javascript files with requirejs (e.g. a specific file in the locale is required)

Some of the jquery plugins we rely on offer the ability to include locale / culture files so that non-user users feel at home with their functionality (e.g. jquery-globalize and bootstrap-datapicker).

The old school way of achieving this was as follows (where "en-AU" is defined "on the fly", sometimes resulting in 404 for missing cultures):

<script type="text/javascript" src="/js/globalize/globalize.js"></script> <script type="text/javascript" src="/js/globalize/cultures/globalize.culture.en-AU.js"></script> 

Is there a recommended way to achieve this using requirejs (note that globalization is included as a pad)?

Here is my first attempt, not yet sure how optomizer requirejs will handle this ...

 Globalize = require("globalize"); ... locale = module.config().locale; if (locale != null) { require(["globalize/globalize.culture." + locale], function() { logger.debug("Loaded locale '" + locale + "'"); Globalize.culture(locale); }, function() { logger.debug("Unable to load locale '" + locale + "'"); }); } 

edit: the optimizer does an excellent job of this, but the solution doesn’t actually work, because the culture file is loaded asynchronously, maybe the application used globalization before installing the culture.

+4
source share
1 answer

Kendo does something similar in its code.

This is a small modification of their technique. This particular style allows you to load modules either through the required or direct inclusion of the script.

I modified it a bit to configure prereqs. On the other hand, this code will no longer work through the r.js. optimizer.

 var myLocalPrereqs = ['file_english.min']; ("function" == typeof define && define.amd ? define : function (preReqs, func) { return func() })(myLocalPrereqs, function () { (function () { // Your module code goes here... })() }); 
0
source

Source: https://habr.com/ru/post/1483272/


All Articles