Dojo.requireIf does not allow local variables

dojo

I've been trying to use dojo.require(If) with a local variable to dynamically load a module on a page based on a condition.

// note: dojo v1.4
djConfig = {
  debugAtAllCosts: true
};

Example 1 (does not work):

(function() {
  var nameOfClass = "Two";
  dojo.require("my.namespace." + nameOfClass);
  dojo.addOnLoad(function() {
    var oneOrTwo = new my.namespace[nameOfClass]();
  });
}());

Error: ReferenceError: nameOfClass is not defined.

Example 2 (does not work):

(function() {
  var nameOfClass = "Two";
  dojo.requireIf(nameOfClass == "One", "my.namespace.One");
  dojo.requireIf(nameOfClass == "Two", "my.namespace.Two");
  dojo.addOnLoad(function() {
    var oneOrTwo = new my.namespace[nameOfClass]();
  });
}());

Error: ReferenceError: nameOfClass is not defined.

Example 3 (works):

(function() {
  window.nameOfClass = "Two";
  dojo.requireIf(window.nameOfClass == "One", "my.namespace.One");
  dojo.requireIf(window.nameOfClass == "Two", "my.namespace.Two");
  dojo.addOnLoad(function() {
    var oneOrTwo = new my.namespace[nameOfClass]();
  });
}());

For some reason, it appears as though require and requireIf only allow global variables inside them. Is that a current limitation, or am I just doing something wrong?


Update 1:

Therefore, if I understand you (@Maine, @jrburke) correctly, this is a limitation of the debugAtAllCosts? If the above code is built as cross-domain (adding the xd file prefix / suffix) and is executed — it will work as expected?

If that is the case, then what is the proper way of locally testing code that will be executed as cross-domain, without making the actual build?

That also makes me question the motivation for pre-parsing the dojo.require(s). If the loader_xd will not (or rather can not) pre-parse, why is the method that was created for testing/debugging doing so?

Update 2:

Since the two questions in the Update 1 above are not closely related to this one, I've moved them out into a separate discussion.

Best Answer

This is because requireIfs are parsed with regexps as the very first thing, and executed before the normal program flow.

If you'll grep Dojo source for requireIf, you should find this kind of lines handling it (loader_xd.js):

var depRegExp = /dojo.(require|requireIf|provide|requireAfterIf|platformRequire|requireLocalization)\s*\(([\w\W]*?)\)/mg;

The condition is then executed with eval in global scope, and not as a part of normal flow.

Related Topic