I set the data-main for Requirejs and according to the documentation that should set the baseUrl for all my script files. But this is not the case. My folder structure is this:
Home/Index.html
Content/scripts/main.js
Content/scripts/libs/require/require.js
Content/scripts/libs/jquery/require_jquery.js
Content/scripts/libs/jquery/jquery-1.7.1.mins.js
Here is the script tag in my Index.html:
<script data-main="/PAWS/Content/scripts/main.js" src="/PAWS/Content/scripts/libs/require/require.js" type="text/javascript"></script>
I would assume it would set my baseUrl to /PAWS/Content/scripts/ but its not working for me. In my main.js I do this:
require(
{ paths:
{ jquery: 'libs/jquery',
knockout: 'libs/knockout'
}
},
['jquery/require_jquery'],
function ($) { .... }
);
In my require_jquery.js file I do this:
define(["libs/jquery/jquery-1.7.1.min.js"], function () {
return jQuery;
});
But I get a 404 error saying that:
GET http://localhost/PAWS/Home/libs/jquery/jquery-1.7.1.min.js 404 NOT FOUND
You see.. my baseUrl should be /PAWS/Content/scripts... But it totally ignores my data-main attribute setting and just resolves /PAWS/Home/ to be the baseUrl. What am I doing wrong?
RequireJS is, in web-terms, an old technology now (some might say ancient), but it is still in wide use and there have been a number of questions about RequireJS and DataTables recently.
It is used by RequireJS to know which module to load in your application. For instance − <script data-main = "scripts/main" src = "scripts/require.js"></script> 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.
Is RequireJS synchronous? 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.
shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. Here is an example. It requires RequireJS 2.1. 0+, and assumes backbone. js, underscore.
From the RequireJS API docs:
However, if the dependency name has one of the following properties, it is treated as a regular file path, like something that was passed to a
<script src="">
tag:
- Ends in ".js".
- Starts with a "/".
- Contains an URL protocol, like "http:" or "https:".
From this, it appears that your explicit ".js" on the end of libs/jquery/jquery-1.7.1.min.js
is confounding your path re: baseUrl. Try libs/jquery/jquery-1.7.1.min
instead.
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