Why isn't my block including Javascript ???

Why isn't my block including Javascript ???

by Cali Dude -
Number of replies: 3
Hi fellow developers,
I'm new to moodle development. I included Javascript following the guidelines, but It's not working

Here is my block content code below, I added javascript in blocks/coursetopics/amd/src/myfile.js, the javascript content of my code is below the php code

  May I know where I went wrong. I also included the content of my console on chrome at the bottom

   public function get_content() {
        global $OUTPUT, $DB, $PAGE, $CFG;
        $CFG->cachejs = false;
        $PAGE->requires->js_call_amd('block_coursetopic/myfile', 'init');

        if ($this->content !== null) {
            return $this->content;
        }

        $this->content = new stdClass();
        $this->content->footer = '';

        // Add logic here to define your template data or any other content.
        $courses = $DB->get_records_sql('SELECT id, fullname FROM mo_course');
        $dropdown ='<select id="select_course">';
        $dropdown .='<option value="">Select Course Topic</option>';
        foreach($courses as $course) {
            if($course->id == 1) {
                continue;
            }
           $dropdown .="<option value='".$course->id."'>".$course->fullname."</option>";
        }
        $dropdown .="</select>";
       

        $this->content->text = $dropdown;

        return $this->content;
    }

  Javascript myfile.js content


define(['jquery'], function($) {
 
    return {
        init: function() {
            // Put whatever you like here. $ is available
            // to you as normal.
            $("#select_course").change(function(e) {
                var course_id = $(this).val();
                alert(course_id);
            });
        }            
    };
});

Console error

Console error




Average of ratings: -
In reply to Cali Dude

Re: Why isn't my block including Javascript ???

by Justin Hunt -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Are you loading the javascript from somewhere? Moodle will not automatically do that for you.
You need to:
i) put the js file into the plugin's amd/src folder
ii) build it ... something like
cd amd
grunt build

iii) In your PHP code you need to actually load it up
$opts =array('foo'=>'bar');
$PAGE->requires->js_call_amd("block_myblock/myfile", 'init', opts);

See more here: https://moodledev.io/docs/guides/javascript#including-from-php
Average of ratings: Useful (1)
In reply to Justin Hunt

Re: Why isn't my block including Javascript ???

by Cali Dude -
Hi Justin,

I included that part of the code which adds javascript with PHP, in the initial post, I think you missed it.

What I didn't go was to build it. The instructions on Javascript in moodle never mentioned building it. How would you figure it out if you are new to developing in Moodle and it's never mentioned ? Or did I miss the part of the documents that mention it ? I would greatly appreciate if you can give me the link that mentions the building of the javascript content in amd.
In reply to Cali Dude

Re: Why isn't my block including Javascript ???

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This is a very good point. The information you need exists on the Javascript Modules page of the docs, but not on the page you were looking at. I have raised an issue about this so it can be improved.

Average of ratings: Useful (1)