YUI to AMD conversion

YUI to AMD conversion

by Clem Smith -
Number of replies: 3

I'm the maintainer for the Mass Actions block. I'm trying to convert it from YUI to jQuery/AMD so that it's compatible with Moodle when YUI is fully removed. Right now, the block has two javascript files; one of them (js/module_selector.js) defines a class that the other file (/module.js) instantiates. I tried not including an init function in module_selector.js, but Moodle kept throwing errors saying "unexpected token" and the unexpected token was an opening parenthesis from what looked like an auto-generated init function.

So, I added the init function and that stopped. Of course, the next problem was that module.js couldn't find the class to instance it, even though both files were added with a js_call_amd() call. In an effort to get this working, I've moved all the class logic into the define() call in module.js, but this really violates the single responsibility principle.

So, I'm looking for a little help in the form of any tips anyone can offer on getting this conversion completed successfully. I've been using the following pages for reference in this effort:
https://docs.moodle.org/dev/Javascript_Modules
https://docs.moodle.org/dev/jQuery


I suspect there's something on one or both of those pages that I am not correctly understanding and, obviously, I've yet to figure out what that is. Thank you in advance!

Average of ratings: -
In reply to Clem Smith

Re: YUI to AMD conversion

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ditch using modules.js and instead have one AMD file (instantiated with a $PAGE->requires->js_call_amd) which has the logic of modules.js that then has an AMD dependency on the logic in module_selector.js - which in turn is a separate AMD module in the block.  Sort of like https://github.com/gjb2048/moodle-theme_shoehorn/blob/master/amd/src/shoehorn_chart.js#L2 where it depends on a minified (no src version) file: https://github.com/gjb2048/moodle-theme_shoehorn/blob/master/amd/build/chartist.min.js and is all loaded by: https://github.com/gjb2048/moodle-theme_shoehorn/blob/master/layout/tiles/additionaljs.php#L89.
Average of ratings: Useful (2)
In reply to Clem Smith

Re: YUI to AMD conversion

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
+1 for ditching module.js and just using AMD.


I tried something like what you are doing a while back, and even if you get it to work its fundamentally the wrong direction. The whole idea of AMD is to encapsulate the code related to a module and its dependencies. Trying to call into that from outside is hard work and kind of pointless.