MDL-64061 javascript: Allow js_call_amd() be called without a function
Not all AMD modules need an explicit function call to start working. The patch makes the function name optional, in which case the js_call_amd() simply loads the module.
This commit is contained in:
@@ -102,4 +102,31 @@ class core_outputrequirementslib_testcase extends advanced_testcase {
|
||||
// Make sure that the generated code does not contain backslashes.
|
||||
$this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test AMD modules loading.
|
||||
*/
|
||||
public function test_js_call_amd() {
|
||||
|
||||
$page = new moodle_page();
|
||||
|
||||
// Load an AMD module without a function call.
|
||||
$page->requires->js_call_amd('theme_foobar/lightbox');
|
||||
|
||||
// Load an AMD module and call its function without parameters.
|
||||
$page->requires->js_call_amd('theme_foobar/demo_one', 'init');
|
||||
|
||||
// Load an AMD module and call its function with some parameters.
|
||||
$page->requires->js_call_amd('theme_foobar/demo_two', 'init', [
|
||||
'foo',
|
||||
'keyWillIgnored' => 'baz',
|
||||
[42, 'xyz'],
|
||||
]);
|
||||
|
||||
$html = $page->requires->get_end_code();
|
||||
|
||||
$this->assertContains('require(["theme_foobar/lightbox"]);', $html);
|
||||
$this->assertContains('require(["theme_foobar/demo_one"], function(amd) { amd.init(); });', $html);
|
||||
$this->assertContains('require(["theme_foobar/demo_two"], function(amd) { amd.init("foo", "baz", [42,"xyz"]); });', $html);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user