DataJS库未在RequireJS中加载 [英] DataJS library not loading in RequireJS

查看:100
本文介绍了DataJS库未在RequireJS中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是RequireJS的新手,所以这可能是一个愚蠢的问题!

I am new to RequireJS, so this might be a stupid question!

我正在使用require-jquery。

I am using require-jquery.

我想将 DataJS 库作为模块加载。它是一个独立的库,不依赖于jQuery。

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

这是我的HTML文件start.htm的样子:

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>

这是Loader.js文件的样子:

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

这是我的文件结构:

Project
|
|----- start.htm
|
|----- Scripts  
       |
       |----- datajs-1.1.0.js   
       |
       |----- require-jquery.js
       |
       |----- loader.js

我认为datajs库支持AMD ,因为这就是库的样子:

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

我做错了什么?

推荐答案

使用requirejs我有这个代码:

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和OData对象是可访问的。

Here the datajs and the OData objects are accessible.

Personaly我认为在同一个文件中有多个'paths'条目有点尴尬..

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

这篇关于DataJS库未在RequireJS中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆