ARMANDO SILVA ARMANDO SILVA - 2 years ago 511
jQuery Question

Javascript Moodle

Hi I am fairly new to moodle. I have been trying to test if my Javascript runs but to no avail. Here is what I have:

In /videojs/amd/src I made a test.js file with a simple command

define(['jquery'], function() {
return {
init: function() {
// Put whatever you like here. $ is available
// to you as normal.
alert("It changed!!");
}
};
});


Then I grunt the file and everything succeed, and made minified. But when I go to the page it doesn't run. Now I read Moodle's Javascript Doc and I see it says

The idea here is that we will run the 'init' function from our (PHP) code to set things up. This is called from PHP like this...


Where do I call this PHP?

Answer Source

Somewhere in the page you are outputting, you need to add a call like this:

$PAGE->requires->js_call_amd('PLUGINTYPE_videojs/test', 'init);

It's not entirely clear from your example what sort of plugin you are creating, so whichever type you are creating (https://docs.moodle.org/dev/Plugin_types), you need to put it in the appropriate subdirectory for your site (e.g. /mod/videojs, /local/videojs, etc.), then add some sort of PHP script as the entry point for your plugin and call the js_call_amd function from there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download