Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Useful core Javascript modules: Difference between revisions

From MoodleDocs
(Created page with "{{Moodle 2.9}} = Useful core Javascript Modules = == Configuration settings (core/config) == Example of using config module: <code javascript> require(['core/config’...")
 
No edit summary
Line 1: Line 1:
{{Moodle 2.9}}
{{Moodle 2.9}}
= Useful core Javascript Modules =


== Configuration settings (core/config) ==
== Configuration settings (core/config) ==
Line 8: Line 5:
Example of using config module:
Example of using config module:
<code javascript>
<code javascript>
    require(['core/config’], function(mdlcfg) {
require(['core/config’], function(mdlcfg) {
        cosole.log(mdlcfg.wwwroot); // outputs the wwwroot of moodle to console
    cosole.log(mdlcfg.wwwroot); // outputs the wwwroot of moodle to console
    });
});
</code>
</code>


Line 17: Line 14:
Example of using language strings module (retrieved via ajax, if the string is not yet loaded)
Example of using language strings module (retrieved via ajax, if the string is not yet loaded)
<code javascript>
<code javascript>
    require([‘core/str’], function(str) {
require([‘core/str’], function(str) {
        str.get_string('edita', 'core', stringargument).done(function(s) {
    str.get_string('edita', 'core', stringargument).done(function(s) {
          console.log(s);
      console.log(s);
        }).fail(console.log(e));
    }).fail(console.log(e));
    });
});
</code>
</code>


Line 30: Line 27:
$PAGE->requires->string_for_js('edita', 'core');
$PAGE->requires->string_for_js('edita', 'core');
</code>
</code>
==See also==
* [[Javascript Modules]]
[[Category:AJAX]]
[[Category:Javascript]]

Revision as of 02:01, 27 July 2015

Moodle 2.9


Configuration settings (core/config)

Example of using config module: require(['core/config’], function(mdlcfg) {

   cosole.log(mdlcfg.wwwroot); // outputs the wwwroot of moodle to console

});

Language strings (core/str)

Example of using language strings module (retrieved via ajax, if the string is not yet loaded) require([‘core/str’], function(str) {

   str.get_string('edita', 'core', stringargument).done(function(s) {
      console.log(s);
   }).fail(console.log(e));

});

The string will be retrieved via AJAX request on the first use. To avoid ajax call you may preload strings in PHP:

global $PAGE; $PAGE->requires->string_for_js('edita', 'core');

See also