Topic on Talk:ResourceLoader/Migration guide for extension developers

Jump to navigation Jump to search
Krinkle (talkcontribs)

I'm trying to make an extension compatible with the ResourceLoader. All I have is one .js file to include. Here's what I have so far - (pastebin). Registration seems to work, my page source has:

if ( window.mediaWiki ) { mediaWiki.loader.load( ["mediawiki.legacy.edit","ext.Hi","ext.wikiEditor.toolbar", ... ] ); }

but the javascript functions aren't being loaded. Firefox error console clearly states the function is not defined. What am I missing? --MrC 22:06, 12 October 2010 (UTC)

This post was posted by Krinkle, but signed as MrC.

Krinkle (talkcontribs)

You are probably operating under the assumption that your code is being run in the global scope. ResourceLoader encapsulates code in closures which are executed as soon as all of their dependencies are met, meaning that all of the functions and variables you created are only defined within the scope of the closure. If you want to export functions or variables to the global scope, just use

window.myMethod = function() { ... };

or

window.myVariable = 'some value';

You will be able to refer to them as you normally would from there on out. I will note however that polluting the global scope is tempting, but you should seriously consider wither making your code a jQuery plug-in or following the model of mediaWiki and jQuery objects, in which your entire library is encapsulated in a single object. --Trevor Parscal 00:09, 13 October 2010 (UTC)

This post was posted by Krinkle, but signed as Trevor Parscal.

Krinkle (talkcontribs)

Ok, thanks, that makes sense. Where would the calls to window.myMethod go? I would certainly like to use the MW model, but so far I can find very little documentation on why/when/how to do it. I'm still not sure if I should even be using ResourceLoader. Here's my situation- I have a parser extension that converts my custom tag into some code which makes calls to myExt.js, which is short and only serves this one tag. So any page that has this tag needs to access myExt.js. On a non-MW page, I would just add "script src=myext.js" to the head. What should I do in MW, and how? MrC 15:20, 14 October 2010 (UTC)

This post was posted by Krinkle, but signed as MrC.

Krinkle (talkcontribs)

I understand the conception not to pollute the global namespace, and I want to use this feature of RL, but how? I've already read all of the documentation you provided, but I swear, I didn't find anything related. There's a mediaWiki.loader class and nothing more I can do with that. How can I have the browser recognized my function calls automatically? -- Szotsaki 10:25, 23 July 2011 (UTC)

This post was posted by Krinkle, but signed as Szotsaki.

Reply to "Getting started"