Last modified: 2014-11-05 14:49:11 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T75011, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 73011 - resourceloader: Function defined javascript function does not exist in global scope
resourceloader: Function defined javascript function does not exist in global...
Status: RESOLVED INVALID
Product: MediaWiki
Classification: Unclassified
ResourceLoader (Other open bugs)
1.23.4
All All
: Unprioritized major (vote)
: ---
Assigned To: Nobody - You can work on this!
: javascript
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2014-11-05 09:14 UTC by Mayank Tiwari
Modified: 2014-11-05 14:49 UTC (History)
4 users (show)

See Also:
Web browser: Firefox
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments
javascript reference error for custom function (211.11 KB, image/png)
2014-11-05 09:14 UTC, Mayank Tiwari
Details

Description Mayank Tiwari 2014-11-05 09:14:16 UTC
Created attachment 17028 [details]
javascript reference error for custom function

I am developing an extension, let us say, "example" extension. I have added the javascript file for this extension let us say example.js using ResourceLoader module.

I have defined a custom function in this js file here is a test structure

example.js
*************************
function testFunction(){}
alert("hi");
************************

When Page loads the alert("hi") is executed that means the js file is there but when I try to call the testFunction on onclick or any other event I get error,

But when I do the same by placing file like
addHTMl('<script src="URL-/extensions/example/resources/example.js"></script>');

the function call works well, Don't know what to do.
Comment 1 Andre Klapper 2014-11-05 12:33:53 UTC
If you assume that there is a software bug in MediaWiki, could you provide a minimal self-contained testcase?
Comment 2 Krinkle 2014-11-05 14:10:28 UTC
Executing files in the global scope is considered an anti-pattern in modern web development and causes all kinds of interoperability and maintenance problems.

ResourceLoader modules are wrapped in a closure (one step away from global in lexical scoping) to discourage use of global variables.

Variables and functions are local to the module by default, not global for the entire browser execution context.

To expose a method or property to the public (e.g. so that other modules can reference them), attach them to a host object (e.g. jQuery, mediaWiki or your own application singleton).

e.g.

-- extensions/MyApp/foo.js

mw.foo = function () { return 4; };
// or:
window.MyApp = {};
MyApp.foo = function () { return 4; };

These will all be exposed and accessible from the global scope.

-- extensions/OtherApp/bar.js
-- dependencies: ext.myApp.foo

alert(mw.foo());
Comment 3 Mayank Tiwari 2014-11-05 14:33:26 UTC
So you mean to say I shall write it like

mw.testFunction() = function(){ };

or say

mw.testFunction = function(a,b){ return a+b; };

It won't cost me referenceError in ResourceLoader ? See this might have been a silly problem as per your prespective but I tried so much to find solution, even I went to IRC but nobody replied so finally I was left to assume that its a bug.

I owe you apology in case it was not appropirate but I'll try it tomorrow and will let you know if it worked.
Comment 4 Krinkle 2014-11-05 14:47:56 UTC
(In reply to Mayank Tiwari from comment #3)
> So you mean to say I shall write it like
> 
> mw.testFunction() = function(){ };
> 
> or say
> 
> mw.testFunction = function(a,b){ return a+b; };
> 
> It won't cost me referenceError in ResourceLoader ?

Indeed. Though I'd recommend grouping them in a container, e.g.

mw.myApp = {};
mw.myApp.testFunction = function(){
};
mw.myApp.testFunction2 = function(){
};

// Or:
mw.myApp = {
  testFunction: function(){
  },
  testFunction2: function(){
  }
};
Comment 5 Krinkle 2014-11-05 14:49:11 UTC
(In reply to Mayank Tiwari from comment #3)
> I owe you apology in case it was not appropriate but I'll try it tomorrow
> and will let you know if it worked.

No worries. We all learn every day.

Do note that this isn't specific to MediaWiki or Resourceloader. The concept of closures is part of how javascript works and quite common to developing front-end web applications in general.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links