Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require参数使用变量名时就不好使了 #4

Closed
killeryyl opened this issue Mar 21, 2013 · 3 comments
Closed

require参数使用变量名时就不好使了 #4

killeryyl opened this issue Mar 21, 2013 · 3 comments

Comments

@killeryyl
Copy link

正常文档里使用
var name = 'book/List';
require(name);
这一步就不好用了。。。
特别在er里使用loadSubAction时,若简明的复用loadSubAction方法,提供action对应的name,那么require(name)应该能正确返回值的。
请看看这个是否需要更改。

@errorrik
Copy link
Contributor

If the dependencies argument is omitted, the module loader MAY choose to scan the factory function for dependencies in the form of require statements (literally in the form of require("module-id")).

@killeryyl
Copy link
Author

抱歉,我是在define里用的动态加载概念。代码里不明确指定需要哪些模块,而是根据交互后提供的参数来确定加载模块。这样用变量名的require方式就不能被正确的预处理加载(这倒是保证了里里外外的模块都能加载完毕),使用的时候会出错。如果判断不存在时在尝试加载,貌似也不是什么好方法,目前也不大肯定这里值不值得动刀。看以后业务里的使用情况吧。

@errorrik
Copy link
Contributor

抱歉,我是在define里用的动态加载概念。代码里不明确指定需要哪些模块,而是根据交互后提供的参数来确定加载模块。这样用变量名的require方式就不能被正确的预处理加载(这倒是保证了里里外外的模块都能加载完毕),使用的时候会出错。如果判断不存在时在尝试加载,貌似也不是什么好方法,目前也不大肯定这里值不值得动刀。看以后业务里的使用情况吧。

如果是希望动态的话,应该使用require({Array}modules, {Function}callback)接口

但是我们曾经讨论过这个问题,最后认为,碰到这种场景,大多数情况应该认为当前模块依赖于两个模块,都加载。也就是:

var mod;
if (...) {
    mod = require('modA');
}
else {
    mod = require('modB');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants