Quantcast
Channel: General developer forum
Viewing all articles
Browse latest Browse all 37677

js_init_call returning "Cannot call method" in block plugin

$
0
0
by Matt Bond.  

Hi, I'm looking for some help resolving a javascript issue with a block plugin I'm working on.

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. Could someone point me in the right direction here or tell me what I am 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;
};

Viewing all articles
Browse latest Browse all 37677

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>