function drawmap not defined when loading external js with resourceLoader

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

function drawmap not defined when loading external js with resourceLoader

Tom Schulze-2
Hello everyone,

I have created a public gist
<https://gist.github.com/tomschulze/afdda960b8c63a3fde0db6cb2ceea0ae>
with a minimal working example. I am trying to put JavaScript Code from
a Widget to an external JS file and load it dynamically with the
ResourceLoader. However, the developer console returns the following
error: jQuery.Deferred exception: drawmap is not defined when I am
accessing the function defined in the external script. Checking the
state in the developer console with
'mw.loader.getState("ext.iswiki.display_project_map")' returns: "ready".

When the Resource Loader debug mode is enabled in LocalSettings.php the
function is found and called.

What am I doing wrong?

Kind regards,

Tom

_______________________________________________
Wikitech-l mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Reply | Threaded
Open this post in threaded view
|

Re: function drawmap not defined when loading external js with resourceLoader

Bartosz Dziewoński
Scripts loaded via ResourceLoader are not executed in global scope, so
your variables/functions will not be global.

To make them global, you have to explicitly attach them to the `window`
object – add this at the end of your external_script.js:

     window.drawmap = drawmap;

--
Bartosz Dziewoński

_______________________________________________
Wikitech-l mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/wikitech-l