I am new to RequireJS, so this might be a stupid question!
I am using require-jquery.
I want to load the DataJS library as a module. It is a standalone library and does not depend on jQuery.
This is what my HTML file start.htm looks like:
<html>
<head>
</head>
<body>
<script type="text/javascript" src="Scripts/Loader.js"></script>
</body>
</html>
This is what the Loader.js file looks like:
(function (window, undefined) {
var script = document.createElement('script');
script.async = true;
script.src = "scripts/require-jquery.js";
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(script, entry);
script.onload = script.onreadystatechange = function () {
var rdyState = script.readyState;
if (!rdyState || /complete|loaded/.test(script.readyState)) {
require([
"jquery",
"scripts/datajs-1.1.0"
],
function (jQueryHandle, odata) {
alert(odata);
});
script.onload = null;
script.onreadystatechange = null;
}
};
})(window);
This is my file structure:
Project
|
|----- start.htm
|
|----- Scripts
|
|----- datajs-1.1.0.js
|
|----- require-jquery.js
|
|----- loader.js
I think that the datajs library supports AMD, because this is what the library looks like:
(function (window, undefined) {
var datajs = window.datajs || {};
var odata = window.OData || {};
// AMD support
if (typeof define === 'function' && define.amd) {
define('datajs', datajs);
define('OData', odata);
} else {
window.datajs = datajs;
window.OData = odata;
}
/* -------------------- */
})(this);
What am I doing wrong?
With requirejs I have this code:
<script type="text/javascript" src="0.1/Clientscripts/requirejs/2.1.11/require.js"></script>
<script type="text/javascript">
requirejs.config({
'baseUrl': '0.1/Clientscripts/',
'paths': {
'datajs':'datajs/1.1.2/datajs.min',
'OData':'datajs/1.1.2/datajs.min'
},
'shim': {
'OData':['datajs']
}
});
</script>
In my own module i did this:
define(['datajs','OData'], function (datajs,OData) {
console.log(datajs);
console.log(OData);
console.log(OData.read);
}
Here the datajs and the OData objects are accessible.
Personaly I believe it's a bit awkward to have multiple 'paths'-entries to the same file..
It would have been cleaner if you could say:
'paths': { 'datajs':'path/to/datajs',...
//and then
require(['datajs/core','datajs/OData'],...
But then again.. nothing is perfect :)
I think thats define([... instead of require([..
来源:https://stackoverflow.com/questions/14842892/datajs-library-not-loading-in-requirejs