I have a moodle block plugin where I'm trying to include javascript in the page.
No matter how I try, the javascript called by the js_init_call always throws an error instead of running I've tried passing a full array to js_init_call, and I've tried the shorthand form that assumes the file is called module.js
If the javascript file doesn't exist, I get a moodle error complaining about the missing file. If it does exist, I get a javascript error as above. What am I doing wrong?
block_foo.php:
<?php
class block_foo extends block_base {
public function init() {
$this->title = get_string('foo', 'block_foo');
}
public function applicable_formats(){
return array('course-view' => true);
}
public function get_content() {
global $PAGE;
if ($this->content !== null) {
return $this->content;
}
if(!isloggedin()){
//if(!is_enrolled()){
return false;
}
$this->content = new stdClass;
$this->content->text = 'asdf';
$this->content->footer = '';
$jsmodule = array(
'name' => 'block_foo',
'fullpath' => '/blocks/foo/foo.js',
'requires' => array(),
'strings' => array()
);
$PAGE->requires->js_init_call('M.block_foo.init', null, false, $jsmodule);
//This style of call doesn't work either... (if the js is named module.js)
//$PAGE->requires->js_init_call('M.block_foo.init', null);
return $this->content;
}
function hide_header(){
return true;
}
}
module.js or foo.js:
M.block_foo = {};
M.block_foo.init = function(){
alert("I was called, yay");
$var = 1234;
$var = $var + 3;
};
This problem, ultimately, was caused by Moodle responding with a HTTP/200 response of size 0 to requests for the javascript file.
That response appeared to have been caused by the module being just a symbolic link to my development directory instead of an actual copy of the files. Why this only happened for the javascript file, and not the php files, I don't know.
So don't develop moodle modules then symbolic-link them into place.