2

我有一个 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;
};
4

2 回答 2

1

这个问题最终是由 Moodle 以大小为 0 的 HTTP/200 响应响应对 javascript 文件的请求引起的。

该响应似乎是由于模块只是到我的开发目录的符号链接而不是文件的实际副本引起的。为什么这只发生在 javascript 文件,而不是 php 文件,我不知道。

所以不要开发moodle模块然后将它们符号链接到适当的位置。

于 2012-10-25T22:15:52.203 回答
0

我知道这是一个老问题,但为了将来参考,使用'fullpath' => new moodle_url($CFG->wwwroot.'/blocks/foo/foo.js'),符号链接不会有任何问题。

于 2014-05-22T00:55:28.003 回答