Note that there are some explanatory texts on larger screens.

plurals
  1. POdojo.requireIf does not allow local variables
    primarykey
    data
    text
    <p>I've been trying to use <code>dojo.require(If)</code> with a local variable to dynamically load a module on a page based on a condition. </p> <pre> // note: dojo v1.4 djConfig = { debugAtAllCosts: true }; </pre> <p><strong>Example 1 (does not work)</strong>:</p> <pre> (function() { var nameOfClass = "Two"; dojo.require("my.namespace." + nameOfClass); dojo.addOnLoad(function() { var oneOrTwo = new my.namespace[nameOfClass](); }); }()); </pre> <p>Error: ReferenceError: nameOfClass is not defined.</p> <p><strong>Example 2 (does not work)</strong>:</p> <pre> (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](); }); }()); </pre> <p>Error: ReferenceError: nameOfClass is not defined.</p> <p><strong>Example 3 (works)</strong>:</p> <pre> (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](); }); }()); </pre> <p>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?</p> <hr> <p><strong>Update 1</strong>:</p> <p>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? </p> <p>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? </p> <p>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? </p> <p><strong>Update 2</strong>:</p> <p>Since the two questions in the Update 1 above are not closely related to this one, I've moved them out into a <a href="https://stackoverflow.com/questions/1922351/dojo-how-to-locally-test-cross-domain-builds">separate discussion</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload