I am using requirejs. My main.js content is like following.
requirejs.config({
async: true,
parseOnLoad: true,
packages: [],
paths: {
jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min'
}
});
require(["login"], function (loginService) {
loginService.login('validUser');
});
Now, my config elements are little. But later, I will add packages, paths and others, so the require.config lines will increase.
RequireJS has been a hugely influential and important tool in the JavaScript world. It's still used in many solid, well-written projects today.
So, RequireJS doesn't support it. From your use case it seems that you don't need synchronous RequireJS, you need to return result asynchronously. AMD pattern allows to define dependencies and load them asynchronously, but module's factory function must return result synchronously.
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
RequireJs is basically a JavaScript for the specific module. RequireJs use AMD Standards. RequireJs don't allow to run global JavaScript. If you have to use JavaScript then add into RequireJS configuration file. RequireJs share code and data between modules and programs.
Yes you can, require your config before you require anything else, like this:
config example:
require.config({
baseUrl: '/Public/js',
paths: {
jquery: '../../Scripts/jquery-1.10.2.min',
jqueryui: '../../Scripts/jquery-ui-1.10.2.min',
},
shim: {
jqueryui: {
deps: ['jquery']
},
}
waitSeconds: 3
});
and, then I load it:
require(['/Public/js/config.js'], function() {
require(['home/index'], function() {
});
});
Just remember that you reference the config.js by path in the first require-statement because require.js can not resolve by baseUrl since it has not been loaded. When you get to the inner require()-statement, its loaded and you can reference dependencies relative to baseUrl.
You can put the config into a separate JS file, that's not a problem. Just make sure that file is loaded prior to the require() call in your main code.
If you're using jQuery for other scripts that are not loaded via requireJS, you will get errors if they happen to load sooner than jQuery. What you need to do is convert all those static files into requireJS modules and load them all via requireJS. By using a define() function in each of the modules, you can set up dependencies, so all modules will wait for jQuery to load prior to executing their own code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With