ฉันมีปลั๊กอิน Moodle Block ที่ฉันพยายามรวม JavaScript ไว้ในหน้าเว็บ
ไม่ว่าฉันจะลองอย่างไร javascript ที่เรียกโดย js_init_call มักจะแสดงข้อผิดพลาดแทนที่จะทำงาน ฉันได้ลองส่งอาเรย์แบบเต็มไปที่ js_init_call และฉันได้ลองใช้รูปแบบชวเลขที่ถือว่าไฟล์นั้นเรียกว่า module.js
หากไฟล์จาวาสคริปต์ ไม่มี อยู่ ฉันได้รับข้อผิดพลาด Moodle ที่แจ้งเกี่ยวกับไฟล์ที่หายไป หาก ไม่มี อยู่ ฉันได้รับข้อผิดพลาดจาวาสคริปต์ดังที่กล่าวข้างต้น ผมทำอะไรผิดหรือเปล่า?
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;
};