RequireJS works for me about half the time. If I refresh the page, it randomly gets load issues.
I'm adapting a small MVC project to use it, and doing something like this: How does RequireJS work with multiple pages and partial views?
I've a common main.js that does the loading from my main _layout.cshtml. And other partial views in widgets and elsewhere that use the technique described above. The example below is from my SignIn page:
<script type="text/javascript">
require(["jquery", "kendo", "domReady!"], function ($, kendo) {
$("#signInForm").kendoWindow({
draggable: false,
width: "500px",
modal: true,
title: "Sign In",
resizable: false
});
});
</script>
It appears that sometimes Chrome loads & processes the inline script before main.js (Where the path mapping and other config is defined). Requiring "domReady" makes no difference.
How do I force this script to wait until require.js & main.js has run?
A less than ideal fallback is to return jQuery to it's global scope and use some kind of custom event. Do you have a better idea? Or is there a way built into RequireJS?
UPDATE: This is the work around I'm using for now in my _layout.cshtml
<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
<script type="text/javascript">
function requireIt(requirements, callback) {
if ($(document).data("requireReady") === true) {
require(requirements, callback);
} else {
$(document).bind("requireReady", null, function () {
$(document).data("requireReady", true);
require(requirements, callback);
});
}
}
</script>
<script data-main="/scripts/main" src="@Url.Content("~/Scripts/require.js")" type="text/javascript"></script>
jQuery returns to being a global fixture and my partial views with their inline scripts use this requireIt wrapper. And main.js triggers requireReady.