Navigation Menu

Skip to content

Commit

Permalink
Resolves LinkedInAtticgh-244 option to disable define.amd
Browse files Browse the repository at this point in the history
Config option and tests added to disable the global define.amd
This change and tests enables a user to mix script tags with their
loader, with an interface to enable/disable the primary form of
detection: define.amd.

This addresses use cases where a user has loaded Inject, and then
places a script tag on the page that tests for define&&define.amd,
attempting to register an anonymous module.

LinkedInAtticgh-244
  • Loading branch information
jakobo committed Apr 10, 2013
1 parent 72ab6f7 commit 87334bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/includes/context.js
Expand Up @@ -185,6 +185,20 @@ context.Inject = {
InjectCore.setUseSuffix(val);
},

/**
* Set the global AMD property. Setting this to "true" can disable
* the global AMD detection. This is really useful in scenarios where
* you anticipate mixing script tags with your loader framework
*/
disableGlobalAMD: function (disable) {
if (disable) {
context.define = Inject.INTERNAL.createDefine(null, null, true);
}
else {
context.define = Inject.INTERNAL.createDefine();
}
},

/**
Clears the local storage caches.
@see InjectCore.clearCache
Expand Down
5 changes: 3 additions & 2 deletions src/injectcore.js
Expand Up @@ -65,13 +65,14 @@ var InjectCore;
* @method InjectCore.createDefine
* @param {string} id - the module identifier for relative module IDs
* @param {string} path - the module path for relative path operations
* @param {boolean} disableAMD - if provided, define.amd will be false, disabling AMD detection
* @public
* @returns a function adhearing to the AMD define() method
*/
createDefine: function (id, path) {
createDefine: function (id, path, disableAMD) {
var req = new RequireContext(id, path);
var define = proxy(req.define, req);
define.amd = {};
define.amd = (disableAMD) ? false : {};
return define;
},

Expand Down
7 changes: 7 additions & 0 deletions tests/integration/tests/anon_define_240.js
Expand Up @@ -11,3 +11,10 @@ test("#240 fail gracefully when an anonymous define is used out of context - AMD
}
ok(pass, "Properly throws an AMD related error as opposed to an untracable issue.");
});

// #244, disable amd detection
test("#244 disable the global AMD, preventing errors", function() {
var pass = false;
Inject.disableGlobalAMD(true);
ok(false === define.amd, "Global AMD detection can be disabled via a config");
});

0 comments on commit 87334bc

Please sign in to comment.