如何用requirejs(在Moodle中)加载Tempus Dominus Bootstrap 4? (错误:没有为datetimepicker定义调用)

时间:2018-09-12 13:44:53

标签: requirejs moodle tempus-dominus-datetimepicker

你好,我在Moodle 3.5中使用requirejs来包含js文件,但是Tempus Dominus Bootstrap 4有问题。

这是我的config.js

define([], function () {
window.requirejs.config({

    paths: {
        "moment": 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min',                   
        "datetimepicker":'https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/js/tempusdominus-bootstrap-4.min',
    },
    shim: {
        'datetimepicker': {deps: ['jquery','moment'], exports: 'datetimepicker'},
    }
});
});

datetimepicker.js

define(['myfolder/config', 'datetimepicker'], function(unused,datetimepicker) {
    return datetimepicker;
}
);

myapp.js

define([
'jquery',
'myfolder/moment',
 'myfolder/datetimepicker',
],
function ($,moment) {
    function initManage() {

它抛出错误:“没有为datetimepicker定义调用”;

怎么了?

1 个答案:

答案 0 :(得分:0)

垫片必须是:

datetimepicker: { exports: "$.fn.datetimepicker"   }

以下是文件: config.js

define([], function () {
  window.requirejs.config({

    paths: {
        "moment": M.cfg.wwwroot + '/admin/tool/myplugin/js/moment.min',
        "moment-fr": M.cfg.wwwroot + '/admin/tool/myplugin/js/moment-fr',
        "bootstrap": M.cfg.wwwroot + '/admin/tool/myplugin/js/bootstrap.bundle.min',
        "datetimepicker":M.cfg.wwwroot + '/admin/tool/myplugin/js/tempusdominus-bootstrap-4',
    },
    shim: {
        bootstrap: { deps: ["jquery"], exports: 'bootstrap'},
       datetimepicker: { exports: "$.fn.datetimepicker"   }
    }
  });
});

datetimepicker.js

define(['tool_myplugin/config',  'datetimepicker'], function(unused,datetimepicker) {
    return datetimepicker;
});

还有app.js

define([
'jquery',
'tool_myplugin/moment',
'tool_myplugin/bootstrap',
'tool_myplugin/datetimepicker'
],
function ($,moment) {

    function initManage() {

        $(document).ready(function () {  
            $('#datetimepicker1').datetimepicker({
                format: 'HH:mm',
                use24hours: true,
                defaultDate: moment({hour: 9,minute:0})
        });  
 ...