Methods
(static) getModuleNames() → {Array.<string>}
(static) getScript(url) → {jQuery.Promise}
Load a script by URL.
Example:
mw.loader.getScript(
'https://example.org/x-1.0.0.js'
)
.then( function () {
// Script succeeded. You can use X now.
}, function ( e ) {
// Script failed. X is not avaiable
mw.log.error( e.message ); // => "Failed to load script"
} );
} );
Parameters:
Name | Type | Description |
---|---|---|
url |
string | Script URL |
- Source:
Returns:
Resolved when the script is loaded
- Type
- jQuery.Promise
(static) getState(module) → {string|null
}
Get the state of a module.
Possible states for the public API:
registered
: The module is available for loading but not yet requested.loading
,loaded
, orexecuting
: The module is currently being loaded.ready
: The module was succesfully and fully loaded.error
: The module or one its dependencies has failed to load, e.g. due to uncaught error from the module's script files.missing
: The module was requested but is not defined according to the server.
Internal mw.loader state machine:
registered
: The module is known to the system but not yet required. Meta data is stored by mw.loader#register. Calls to that method are generated server-side by StartupModule.loading
: The module was required through mw.loader (either directly or as dependency of another module). The client will fetch module contents from mw.loader.store or from the server. The contents should later be received by mw.loader#implement.loaded
: The module has been received by mw.loader#implement. Once the module has no more dependencies in-flight, the module will be executed, controlled via #setAndPropagate and #doPropagation.executing
: The module is being executed (apply messages and stylesheets, execute scripts) by mw.loader#execute.ready
: The module has been successfully executed.error
: The module (or one of its dependencies) produced an uncaught error during execution.missing
: The module was registered client-side and requested, but the server denied knowledge of the module's existence.
Parameters:
Name | Type | Description |
---|---|---|
module |
string | Name of module |
- Source:
Returns:
The state, or null if the module (or its state) is not in the registry.
- Type
-
string
|
null
(static) impl(declarator)
Implement a module given a function which returns the components of the module
Parameters:
Name | Type | Description |
---|---|---|
declarator |
function | The declarator should return an array with the following keys:
The declarator must not use any scope variables, since it will be serialized with Function.prototype.toString() and later restored and executed in the global scope. The elements are all optional except the name. |
- Source:
(static) implement(module, scriptopt, styleopt, messagesopt, templatesopt, deprecationWarningopt)
Implement a module given the components of the module.
See #impl for a full description of the parameters.
Prior to MW 1.41, this was used internally, but now it is only kept for backwards compatibility.
Does not support mw.loader.store caching.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
module |
string | ||
script |
function | Array | string | Object |
<optional> |
|
style |
Object |
<optional> |
|
messages |
Object |
<optional> |
List of key/value pairs to be added to mw#messages. |
templates |
Object |
<optional> |
List of key/value pairs to be added to mw#templates. |
deprecationWarning |
string
|
null
|
<optional> |
Deprecation warning if any |
- Source:
(static) load(modules, typeopt)
Load an external script or one or more modules.
This method takes a list of unrelated modules. Use cases:
- A web page will be composed of many different widgets. These widgets independently
queue their ResourceLoader modules (
OutputPage::addModules()
). If any of them have problems, or are no longer known (e.g. cached HTML), the other modules should still be loaded. - This method is used for preloading, which must not throw. Later code that calls #using() will handle the error.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
modules |
string | Array | Either the name of a module, array of modules, or a URL of an external script or style |
||
type |
string |
<optional> |
'text/javascript' | MIME type to use if calling with a URL of an external script or style; acceptable values are "text/css" and "text/javascript"; if no type is provided, text/javascript is assumed. |
- Source:
Throws:
-
If type is invalid
- Type
- Error
(static) register(modules, versionopt, dependenciesopt, groupopt, sourceopt, skipopt)
Register a module, letting the system know about it and its properties.
The startup module calls this method.
When using multiple module registration by passing an array, dependencies that are specified as references to modules within the array will be resolved before the modules are registered.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
modules |
string | Array | Module name or array of arrays, each containing a list of arguments compatible with this method |
||
version |
string |
<optional> |
Module version hash (falls backs to empty string) |
|
dependencies |
Array.<string> |
<optional> |
Array of module names on which this module depends. |
|
group |
string |
<optional> |
null | Group which the module is in |
source |
string |
<optional> |
'local' | Name of the source |
skip |
string |
<optional> |
null | Script body of the skip function |
- Source:
(static) state(states)
Change the state of one or more modules.
Parameters:
Name | Type | Description |
---|---|---|
states |
Object | Object of module name/state pairs |
- Source:
(static) using(dependencies, readyopt, erroropt) → {jQuery.Promise}
Execute a function after one or more modules are ready.
Use this method if you need to dynamically control which modules are loaded and/or when they loaded (instead of declaring them as dependencies directly on your module.)
This uses the same loader as for regular module dependencies. This means ResourceLoader will not re-download or re-execute a module for the second time if something else already needed it. And the same browser HTTP cache, and localStorage are checked before considering to fetch from the network. And any on-going requests from other dependencies or using() calls are also automatically re-used.
Example of inline dependency on OOjs:
mw.loader.using( 'oojs', function () {
OO.compare( [ 1 ], [ 1 ] );
} );
Example of inline dependency obtained via require()
:
mw.loader.using( [ 'mediawiki.util' ], function ( require ) {
var util = require( 'mediawiki.util' );
} );
Since MediaWiki 1.23 this returns a promise.
Since MediaWiki 1.28 the promise is resolved with a require
function.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dependencies |
string | Array | Module name or array of modules names the callback depends on to be ready before executing |
|
ready |
function |
<optional> |
Callback to execute when all dependencies are ready |
error |
function |
<optional> |
Callback to execute if one or more dependencies failed |
- Source:
Returns:
With a require
function
- Type
- jQuery.Promise