General developer forum

Passing parameters in array when calling the js_call_amd function

 
Picture of tsofiya izchak
Passing parameters in array when calling the js_call_amd function
 
Hi,
I'm trying to run JavaScript from php in a report plugin page I have created, and am running into a problem when passing parameters.
I pass parameters to the JS function using the js_call_amd function:
$PAGE->requires->js_call_amd('report_ortpblit/chart_groups_progress', 'createGraph', $params);
(The JS file is called 'chart_groups_progress.js' and the function to run is 'createGraph'.)
The documentation states that "the $params is an array of params passed as arguments to the function.", but I'm unable to use the $params as an array: When I put data in the 2nd and 3rd offsets ($params[1], $params[2]…), the 'createGraph' function only gets the data in the 1st offset (($params[0]).

How do I use the rest of the array inside the JS function?
 
Average of ratings: -
Picture of Richard Jones
Re: Passing parameters in array when calling the js_call_amd function
Particularly helpful MoodlersPlugin developers

Is this helpful:

js_call_amd takes a third parameter which is an array of parameters. These will translate to individual parameters in the 'init' function call. For example...

$PAGE->requires->js_call_amd('block_iomad_company_admin/department_select', 'init', array($first, $last));

...calls

return {
        init: function(first, last) {
    }
https://docs.moodle.org/dev/Javascript_Modules

So on my reading you are not actually passing an array type but a list of parameters in array format.

If I'm wrong hopefully someone will educate me too.

 
Average of ratings: Useful (3)
Picture of tsofiya izchak
Re: Passing parameters in array when calling the js_call_amd function
 

Hi Richard,

Thank you very much for emphasizing the difference between an array type and a list of parameters in array format. That indeed was our misunderstanding.

Now everything works perfectly.

 smile

Thanks for your helpful answer.


 
Average of ratings: -