Chapter 39. Module Resources
The AMD format allows to define dependencies on a resource loaded by a module based on the loader plug-in.
A module that uses a loader plug-in uses the exclamation mark character (
!) to delineate between the loader plug-in module to use and the target resource to load. Loader plug-ins are useful because they can load resources by means of the AMD loading mechanism and benefit from its performance and parallel loading.
There are some useful loader plug-ins:
- The https://github.com/requirejs/text plug-in for loading of text resources such as stylesheets or templates
- The http://requirejs.org/docs/api.html#i18n plug-in for internationalization bundle support
Procedure 39.1. Configuring a Module Resource
To demonstrate the configuration of an AMD loader plug-in's target resource, we need some HTML code generated by JavaScript. The
text AMD loader plug-in can be used for this purpose. The plug-in will load a template as its resource and pass it as a parameter into a module definition callback.
- Declare
textas a module. It is written as a native module that depends on the predefined RequireJS module calledmodule. The native support mechanism allows this configuration works transparently without modifying the third-party library. <module> <name>text</name> <script> <path>/requirejs/js/plugins/text.js</path> </script> <depends> <!-- module means RequireJS itself --> <module>module</module> </depends> </module>
- Define the
foomodule that requires thetextplug-in to load the template by means of the<resource>XML element. <module> <name>foo</name> ... <depends> <module>text</module> <as>myTemplate</as> <resource>/path/to/template.tmpl</resource> </depends> </module>
- The
foomodule template is loaded by thetextplug-in. (function(myTemplate) { //parse the 'myTemplate' then append to DOM })(myTemplate);