alternative way for include amd code in page

alternative way for include amd code in page

by suman bogati -
Number of replies: 2

Hi Everyone,

As we all know how we can add AMD code in moodle page by PHP,

$PAGE->requires->js_call_amd($modulename, $functionname, $params);

But I need to do this  by JavaScript, because I have a local plugin vm_chat(footer chat). The index.php of it has

$PAGE->requires->js_call_amd('local_vmchat/index', 'init', [$CFG->wwwroot.'/']

By this,  the footer chat would display only in 'chat  plugin' page, but it should display throughout the site.

If I can add this by JavaScript like from Additional HTML page (Dashboard  ► Site administration  ► Appearance  ► Additional HTML),  then the chat would display throughout site.

How can I add AMD code in moodle page by javaScript? 

Or any other suggestion ? You guys may please share.

Thank you




Average of ratings: -
In reply to suman bogati

Re: alternative way for include amd code in page

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Suman,

Take a look at example 3 from https://docs.moodle.org/dev/Javascript_Modules#Embedding_AMD_code_in_a_page

This shows how to do something like this in inline javascript (inserted by PHP).  This gets output wrapped in

require(['core/first'], function() { /* inline code here*/ });
So in your case, *I think* what you want to put in the footer html is something like

<script>
require(['core/first'], function() { require(['local_vmchat/yourmodulename'], function(vmchat) { vmchat.init(); // or whatever function your want your module to run. }); });
</script>

I've not tried this, so someone else might be able to give you a better answer than me, or know a cleaner way to achieve what you're doing.

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: alternative way for include amd code in page

by suman bogati -
Hi Mark,


Thanks for reply,  it's working after inserted the below code in Additional HTML section.


<script type="text/javascript">
 window.onload = function (){
	require(['core/first'], function() {
		require(["local_vmchat/index"], function(amd) { amd.init("https:\/\/local.vidya.io\/moodle30\/"); });;
		require(["core/log"], function(amd) { amd.setConfig({"level":"trace"}); });
	});
}
</script>