Page MenuHomePhabricator

Allow mw.loader.using adding dependency without loading the module
Closed, DeclinedPublic

Description

When manipulate the toolbar with user or site javascript you have to wait until 'mediawiki.action.edit' is "ready", but the code

mw.loader.using( [ 'mediawiki.action.edit' ], function() {
...
});

will load 'mediawiki.action.edit' on each page. Adding a check for wgAction !== edit and submit is a solution, but for other modules you have to know, where it is running and adding that check, that is bad, when the condition is changed.

Having a method inside the loader, which allow executing a function after all dependency are ready without requesting the modules, can help here. When the page is ready and the dependency is not loaded, than the loader ignore this function.

For example:

mw.loader.usingIfExist( [ 'mediawiki.action.edit' ], function() {
...
});

will only executing the function when 'mediawiki.action.edit' is loaded on the page and will not requesting the module itself. When the module is not loaded, the function is ignored.


Version: unspecified
Severity: enhancement

Details

Reference
bz38782

Event Timeline

bzimport raised the priority of this task from to Low.Nov 22 2014, 1:07 AM
bzimport set Reference to bz38782.
bzimport added a subscriber: Unknown Object (MLST).

Modules are always registered, whether they are going to be loaded or not. The reason is because it is not known in advance what will and will not be loaded.

e.g. an inline "mw.loader.load()" should always work. So "if exist" is not possible.

Alternatively "if loaded" could be an option (e.g. mw.loader.callWhenLoaded( ['mediawiki.action.edit'], fn );, which will call the function when that module is loaded, but it would not initiate a load request.

Though it could be useful, I'd argue the only use case it serves are work-arounds that can and should be solved other wise. If there are non-hacky use cases for it, I'd be happy to consider it though.

In the case of the edit toolbar the proper solution imho is to use a hook.