DataJS library not loading in RequireJS

[亡魂溺海] 提交于 2019-12-10 10:04:36

问题


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?


回答1:


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 :)




回答2:


I think thats define([... instead of require([..



来源:https://stackoverflow.com/questions/14842892/datajs-library-not-loading-in-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!