General developer forum

AMD Module Question

 
Picture of Amy Stewart
AMD Module Question
 

I have written an AMD module and am including it in my block using  the following code inside my get_content() function:

$this->page->requires->js_call_amd('block_createticket/sendmail', 'init');


When a page that contains the block loads, the amd module gets included in the resultant HTML twice:

require(["block_navigation/navblock"], function(amd) { amd.init("4"); });;
require(["block_settings/settingsblock"], function(amd) { amd.init("5", "expandable_branch_71_siteadministration"); });;
require(["block_createticket/sendmail"], function(amd) { amd.init(); });;
require(["block_createticket/sendmail"], function(amd) { amd.init(); });;

How should I be adding the AMD module to my block? I have tried to add it using the get_required_javascript() function as well.  That gave me the same result.


Thanks!

Amy

 
Average of ratings: -
Davo
Re: AMD Module Question
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Does your block 'get_content()' function start with the standard code:

if ($this->content !== null) {
   return $this->content;
}

If not, then the rest of your get_content() function will be called twice - once to see if there is any content (otherwise the block is not drawn at all), then the second time to return the content to be output inside the block.

Also, if your block can appear multiple times on the page and you only want to initialise the AMD module once, then you should use a static variable to record whether or not the AMD module has been initialised, preventing it from happening more than once.


 
Average of ratings: Useful (2)
Picture of Amy Stewart
Re: AMD Module Question
 
That was the problem!  Thanks so much for helping.
 
Average of ratings: -