Search code examples
requirejsmoodletempus-dominus-datetimepicker

How to load Tempus Dominus Bootstrap 4 with requirejs (in Moodle) ? ( Error: No define call for datetimepicker)


Hello I use requirejs in Moodle 3.5 to include js files, but I have a problem with Tempus Dominus Bootstrap 4.

Here is my 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() {

And it throws the error: "No define call for datetimepicker";

What is wrong?


Solution

  • The shim has to be:

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

    Here are the files: 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;
    });
    

    And 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})
            });  
     ...