将Require.js用于带qunit的脚本

时间:2015-04-17 20:15:25

标签: javascript jquery requirejs qunit

在我的埃里克·艾略特的书中,我找到了这三个代码,但我不明白是否喜欢使用它们:

(1)

define(['ch04/amd1', 'ch04/amd2'],
function myModule(amd1, amd2) {
    var testResults = {
        test1: amd1.test(),
        test2: amd2.test()
    },
    // Define a public API for your module:
    api = {
        testResults: function () {
            return testResults;
        }
    };
    return api;
});

(2)

require(['ch04-amd'], function (amd) {
    var results = amd.testResults();
    test('AMD with Require.js', function () {
        equal(results.test1, true,
            'First dependency loaded correctly.');
        equal(results.test2, true,
            'Second dependency loaded correctly.');
    });
});

(3)

if (typeof define === 'function') {
    define([], function () {
        return api;
    });
}

我尝试使用require.js并编写这些代码,但它不起作用:

的javascript \ librerie \的jquery.js

的javascript \ librerie \ qunit.js

的javascript \ librerie \ require.js

script_libro_eric_elliott.php

<!DOCTYPE html>
<html>
    <head>
        <title>Utilizzo della libreria Require.js</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script data-main="javascript/moduli" src="javascript/librerie/require.js">
        </script>
    </head>
    <body>
        <div>Utilizzo della libreria Require.js</div>
    </body>
</html>

的javascript \模量\ script_libro.js

define(['ch04/amd1', 'ch04/amd2'],
function myModule(amd1, amd2) {
    var testResults = {
        test1: amd1.test(),
        test2: amd2.test()
    },
    // Define a public API for your module:
    api = {
        testResults: function () {
            return testResults;
        }
    };
    return api;
});

的javascript \模量\ main.js

require(['ch04-amd'], function (amd) {
    var results = amd.testResults();
    test('AMD with Require.js', function () {
        equal(results.test1, console.log(true),
            'First dependency loaded correctly.');
        equal(results.test2, console.log(true),
            'Second dependency loaded correctly.');
    });
});

的javascript \ configurazione.js

/* global requirejs */
requirejs.config({
    "baseUrl": "javascript/librerie",
    "paths": {
        "moduli": "../moduli"
    },
    "shim": {
        "script_libro": {
            deps: ["jquery", "qunit"]
        }
    }
});
requirejs(["moduli/main"]);

我想在控制台上编写输出字符串,所以我也编辑了Eric Elliot的脚本。在我的控制台上,我只能阅读:

Error: Script error for: moduli
http://requirejs.org/docs/errors.html#scripterror

请问你能帮帮我吗?


NEW UPDATE


我认为问题出现在script_libro.js中,实际上控制台只在此文件中发现错误。我发给你新密码:

script_libro_eric_elliott.php

<!DOCTYPE html>
<html>
    <head>
        <title>Utilizzo della libreria Require.js</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, 
initial-scale=1.0">
        <script data-main="javascript/configurazione.js" 
                src="javascript/librerie/require.js">
        </script>
    </head>
    <body>
        <div>Utilizzo della libreria Require.js</div>
        <div id="contenitore"></div>
    </body>
</html>

的javascript \模量\ script_libro.js

define(['jquery', 'qunit'], function myModule(amd1, amd2) {
    var testResults = {
        test1: amd1.test(),
        test2: amd2.test()
    },
    api = {
        testResults: function () {
            return testResults;
        }
    };
    return api;
});

的javascript \模量\ main.js

require(['javascript/moduli/script_libro.js'], function (amd) {
    var results = amd.testResults;
    test('AMD with Require.js', function () {
        equal(results.test1, true,
                console.log('First dependency loaded correctly.'));
        equal(results.test2, true,
                console.log('Second dependency loaded correctly.'));
    });
});

的javascript \ configurazione.js

require.config({
    baseUrl: 'javascript/librerie',
    urlArgs: "bust=" + (new Date()).getTime(),
    paths: {
        CartellaModuli: '../moduli',
    },
    shim: {
        script_libro: {
            deps: ['qunit', 'jquery']
        }
    }
});
require(['CartellaModuli/main']);

1 个答案:

答案 0 :(得分:0)

您的data-main应指向模块而不是目录。尝试将其更改为data-main="javascript/moduli/main"

这可能无法解决所有问题,因为您不会在configurazione.js任何地方包含您的配置。