我有一个 moodle 块插件,我试图在页面中包含 javascript。
无论我如何尝试,js_init_call 调用的 javascript 总是抛出错误而不是运行我尝试将完整数组传递给 js_init_call,并且我尝试了假定文件名为 module.js
如果 javascript 文件不存在,我会收到一个 Moodle 错误,抱怨文件丢失。如果它确实存在,我会收到上面的 javascript 错误。我究竟做错了什么?
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 或 foo.js:
M.block_foo = {};
M.block_foo.init = function(){
alert("I was called, yay");
$var = 1234;
$var = $var + 3;
};