【发布时间】:2014-01-15 13:32:18
【问题描述】:
我对 requirejs 有问题,并在条件下导入模块。例如,我有登录页面,其中包含一些模块。有些关闭它们仅在用户登录时使用,另一种在用户未登录时使用。当然,我不希望用户在登录时注册。所以我有两种不同的解决方案:
第一个解决方案:只需要我需要的东西:
define(['jquery', 'dst!/static/dust/frontend/user_menu.dust'], function($, template) {
var handleResponse = function(data) {
if (data.status == 'not_logged') {
require(["user/frontend/register"]);
require(["user/frontend/login"], function(l) {
});
} else {
require(["user/frontend/logout"]);
require(["user/frontend/change_password"]);
require(["user/frontend/profile_details"]);
}
template(data, function(err, html) {
$('#userMenu').html(html);
});
};
$.ajax({
url: '/user/info/',
type: 'GET',
success: handleResponse
});
});
这让我的代码更加易于阅读,但我无法使用 r.js 工具对其进行优化。
第二种解决方案:
define(['jquery', 'dst!/static/dust/frontend/user_menu.dust',
'user/frontend/register', 'user/frontend/login', 'user/frontend/logout',
'user/frontend/change_password',
'user/frontend/profile_details'], function($, template, register, login, change_password, profile_details) {
var handleResponse = function(data) {
if (data.status == 'not_logged') {
register.init();
login.init();
} else {
logout.init();
profile_details.init();
change_password.init();
}
template(data, function(err, html) {
$('#userMenu').html(html);
});
};
$.ajax({
url: '/user/info/',
type: 'GET',
success: handleResponse
});
});
很难阅读。一切都加载到内存中。
也许还有其他选择可以做到这一点?
【问题讨论】:
标签: javascript angularjs requirejs