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