Search code examples
javascriptphprequirejsmoodleamd

AMD module for moodle question_type


I am trying to add some JS to my question type... So I read that AMD modules should be used, and YUI modules are not encouraged for use anymore. I did everything like in the example on the: https://docs.moodle.org/dev/Javascript_Modules But when i try to call my module from php:

$this->page->requires->js_call_amd('block_overview/helloworld','makeItBlue');

i get an error in console (on google chrome):

No define call for block_overview...

I also tried:

$this->page->requires->js_call_amd('qtype_mytype/helloworld','makeItBlue');

but I got same error...

Folder structure:
question/type/mytype/
                   |--amd/
                        |--src
                             |--helloworld.js
                        |--build
                               |--helloworld.min.js

Solution

  • The first one will certainly not work, as it will be looking in blocks/overview/amd/* to find the javascript file 'helloworld.js'.

    The second one should work, except that in the example, the function 'makeItBlue' is not exported - i.e. it is not available outside of the module.

    The only functions that are available are those that are part of the object that is returned by the module. In the example, the object exported is called 'greeting' and the functions it contains are 'formal' and 'informal'. These don't really do anything very useful, but they are callable from outside the module.

    Take a look at the PDF attached to this blog post: http://learn1.open.ac.uk/mod/oublog/viewpost.php?post=164813 for a really good introduction to using AMD modules in Moodle 2.9+.