DataJS library not loading in RequireJS

I'm new to RequireJS, so this might be a silly question!

I am using require-jquery.

I want to load DataJS library as a module. It is a standalone library and does not depend on jQuery.

This is what my HTML start.htm file 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 the datajs library supports AMD because it looks like this:

(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?

+3


source to share


2 answers


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);
}

      

Datajs and OData objects are available here.



Personaly I find it a bit awkward to have multiple "paths" in the same file.

It would be easier if you could say:

'paths': { 'datajs':'path/to/datajs',...
 //and then
require(['datajs/core','datajs/OData'],...

      

But then again .. nothing is perfect :)

+2


source


I think it defines ([... instead of require ([..



0


source







All Articles