3

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?

ashwnacharya
  • 14,601
  • 23
  • 89
  • 112

2 Answers2

2

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

JDC
  • 1,569
  • 16
  • 33
0

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

Gokul Kav
  • 839
  • 1
  • 8
  • 14
  • For the point I'm making here, about what would be better names, it doesn't mather if it is define or require. – JDC Jan 22 '16 at 14:11