dojo.require如果不允许局部变量 [英] dojo.requireIf does not allow local variables
问题描述
我一直在尝试使用本地变量 dojo.require(If)
,以便根据条件在页面上动态加载模块。
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
};
示例1(不工作):
(function() {
var nameOfClass = "Two";
dojo.require("my.namespace." + nameOfClass);
dojo.addOnLoad(function() {
var oneOrTwo = new my.namespace[nameOfClass]();
});
}());
错误:ReferenceError:nameOfClass未定义。
Error: ReferenceError: nameOfClass is not defined.
示例2(不工作)
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]();
});
}());
错误:ReferenceError:nameOfClass未定义。
Error: ReferenceError: nameOfClass is not defined.
示例3(作品)
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]();
});
}());
由于某些原因,它似乎就像require和requireIf只允许其中的全局变量。这是目前的限制,还是我只是做错了?
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?
更新1 :
因此,如果我明白你(@Maine,@jrburke)是否正确,这是debugAtAllCosts的一个限制?如果上述代码构建为跨域(添加xd文件前缀/后缀)并被执行 - 它将按预期工作?
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?
这也让我怀疑预先解析dojo.require的动机。如果loader_xd不会(或者说不能)预解析,为什么为测试/调试创建的方法呢?
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?
更新2 :
由于上述更新1中的两个问题与此不相关,我已将其移至分离讨论。
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.
推荐答案
这是因为 requireIf
解析正则表达式是第一件事,并在正常程序流程之前执行。
This is because requireIf
s are parsed with regexps as the very first thing, and executed before the normal program flow.
如果你要grep Dojo源为requireIf,你应该找到这种行处理它(loader_xd.js):
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;
然后,使用 eval
执行条件全球范围,而不是正常流量的一部分。
The condition is then executed with eval
in global scope, and not as a part of normal flow.
这篇关于dojo.require如果不允许局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!