I'm running into a variety of issues with race conditions and scoping issues with my web application, using require.js for managing dependencies.
I've been attempting to clean up and optimise the codebase, and introduce r.js as a mechanism for controlling the huge number of requests being generated on page loads (loading dependencies).
Question:
What are the consequences of failing to declare Parameters explicitly within either a module definition like this:
define([
"jquery",
"app/modules/validation",
"jquery.chosen"
], function(
$,
Validation,
// note missing parameter
) { //stuff here });
Or a require statement like this:
require([
"jquery",
"app/modules/validation",
"jquery.chosen"
], function(
$,
Validation,
Chosen
) {
// the 'chosen' object here is actually referencing the object defined in the other module
});
I'm finding numerous issues in my application with scoping and I'm wondering if the above might be to blame? It seems like failing to include a parameter allows it to leak scope? Is this possible?
Edit I was trying to avoid this turning into a 'debug my require.js' configuration but here we go:
My config.js
requirejs.config({
paths: {
jquery: 'jquery-shim',
'jquery.chosen':'chosen/chosen.jquery',
'jquery.validity': 'lib/jquery.validity'
},
shim: {
'jquery.chosen': ["jquery"],
'jquery.validity': {
deps: ["jquery"],
exports: "jQuery.fn.validity"
}
}
});
as you can see jQuery is currently aliased to the following - I think this might be the problem:
//jquery-shim.js
define([], function() {
return window.jQuery;
});
When I load require.js in my html it looks like this: As you can see jquery is being loaded outwith require.js - the reason for this is that when loaded with require.js, it does not bind to the DOMContentLoaded event, and hence all other code that binds to jQ.ready, never executes:
<script src="{{ asset('assets/js/jquery/dist/jquery.min.js') }}"></script>
<script src="{{ asset('assets/js/requirejs/require.js') }}"></script>
<script>
require(['app/site_core'], function (common) {
require(['app/default']);
});
</script>