[ALOY-1259] Alloy: The code in alloy.js should be wrapped by app.js to protect global scope
| GitHub Issue | n/a |
|---|---|
| Type | Improvement |
| Priority | None |
| Status | Open |
| Resolution | Unresolved |
| Affected Version/s | Alloy 1.5.1, Alloy 1.7.0 |
| Fix Version/s | n/a |
| Components | n/a |
| Labels | n/a |
| Reporter | Fokke Zandbergen |
| Assignee | Feon Sua Xin Miao |
| Created | 2015-03-30T17:46:01.000+0000 |
| Updated | 2015-07-09T14:48:33.000+0000 |
Description
The code a developer adds to
app/alloy.js will simply be injected into the generated Resources/<os>/app.js file as can be seen in the [template](https://github.com/appcelerator/alloy/blob/master/Alloy/template/app.js).
Unlike the CommonJS modules that make up the rest of an Alloy app, all variables in this file will be part of the global scope.
We could enforce a best practice to not pollute the global scope by wrapping the imported code in a self-executing function like this:
(function(exports) {
__MAPMARKER_ALLOY_JS__
})(this);
This way, the developer can still force to set a global by using exports.foo = "bar"; just like they export variables in controllers.
Warning
This **will** break apps that depend on globals set inalloy.js, other then those using Alloy.Globals.
PR on master: https://github.com/appcelerator/alloy/pull/675