0

我想编写一个在每个 apge 上运行的脚本——它使用我刚刚加载的 jquery 插件——并在加载所有插件后首先运行它。根据迄今为止的评论,这需要在 Require.js 中定义和运行模块......

我在包含我的 Require.js 代码的 CMS 中有页脚部分,因此:

<!-- special version of jQuery with RequireJS built-in -->
script data-main="/addons/shared_addons/themes/base/js/main" src="/addons/shared_addons/themesbase/js/require-jquery.js" type="text/javascript"> /script

...而且我的 main.js 是相当标准的:

// urlArgs: "bust=v1"  // for release
require.config({
    urlArgs: "bust=" + (new Date()).getTime(),
    paths: {
        Corner: "/addons/shared_addons/themes/base/js/jquery.corner",
        JQForm: "/addons/shared_addons/themes/base/js/jquery.form",
        Gritter: "/addons/shared_addons/themes/base/js/jquery.gritter.min",
        JQUI: "/addons/shared_addons/themes/base/js/jquery-ui-1.8.21.custom.min",
        TimePicker: "/addons/shared_addons/themes/base/js/jquery.ui.timepicker",
        DateFormat: "/addons/shared_addons/themes/base/js/date.format",
        countdown: "/addons/shared_addons/themes/base/js/jquery.countdown.min",
        JQMustache: "/addons/shared_addons/themes/base/js/jquery-Mustache",
        Mustache: "/addons/shared_addons/themes/base/js/mustache",
        Kendo: "/addons/shared_addons/themes/base/js/kendo.web.min",
        Chosen: "/addons/shared_addons/themes/base/js/chosen.jquery.min",
        EveryPage: "/addons/shared_addons/themes/base/js/another_doc_ready_script"
    }       
});

require([
'jquery', 
'Corner',
'JQForm',
'Gritter',
'JQUI',
'TimePicker',
'DateFormat',
'countdown',
'JQMustache',
'Mustache',
'Kendo',
'Chosen'
],
[ "EveryPage" ],
function($) {
    //run
    $(function() {
        console.log('get everything we could possibly need');
    });
});

“another_doc_ready_script.js”包含“everypage”模块定义:

console.log('here');
define("EveryPage",
        [
        'jquery', 
        'Corner',
        'JQForm',
        'Gritter',
        'JQUI',
        'TimePicker',
        'DateFormat',
        'countdown',
        'JQMustache',
        'Mustache',
        'Kendo',
        'Chosen'
        ],
    function($) {
console.log('in everypage');
$(document).ready(function() { 
    console.log('in everypage doc ready');
        $( "body").removeClass('nodisplay');
    }); // document ready
console.log('leaving everypage');
   }  // function-define-require
); // define-require

编辑:Bergi 的评论使更有意义。我已经修改了上面的代码,但它仍然无法正常工作。压缩。纳达。没有控制台日志语句或错误。这必须是一个常见的用法,但我没有找到一个简单的例子。

4

1 回答 1

1

文档

模块不同于传统的脚本文件 […]。它可以显式列出其依赖项 […]。

[那] 允许它们尽可能快地加载,即使是乱序的,但以正确的依赖顺序进行评估。

§ 1.2.3 节描述了如何定义这样一个具有依赖关系的模块。

于 2012-08-04T02:08:30.027 回答