我定义了两个用户角色,即
1:roleId用于管理员用户,2:roleId用于普通用户
我尝试的逻辑是用两个javascript代码文件(.js文件如下)编写的:
第一个文件:prj.js
var ref = new Firebase("https://prj.firebaseio.com");
var loggedInUser = [];
$(document).ready(function() {
authData=ref.getAuth();
if(authData == null){
//TODO find an elegant way to manage authorization
// window.location = "../index.html";
}else{
ref.child("users").child(authData.uid).on("value", function(snapshot){
$( "span.user-name").html(snapshot.val().displayName);
loggedInUser.displayName = snapshot.val().displayName;
loggedInUser.roleId = snapshot.val().roleId;
});
}
});
第二个文件:navigation.js
appraisalMenuHTML = ' <li class="{{appraisal}}" class="dropdown"><a class="dropdown-toggle appraisal"'
+' data-toggle="dropdown" href="/prj/pages/appraisal/appraisalDashboard.html"><span id="appraisal"> Appraisal</span></a>'
+' <ul class="dropdown-menu">'
+' <li><a href="/prj/pages/appraisal/appraisalDashboard.html">Submit review </a></li>'
+ ' </ul></li>';
userOptionsHTML = ' <ul class="nav navbar-nav navbar-right">'
+' <li class="dropdown"><a class="dropdown-toggle"'
+' data-toggle="dropdown" href="#"><img src="/prj/images/settings.png"/></a>'
+' <ul class="dropdown-menu">'
+' <li><a href="/prj/pages/appraisal/reports.html"> Appraisal</a></li>'
+' </ul>'
+ '</li>'
+' <li class="dropdown"><a class="dropdown-toggle user-name"'
+' data-toggle="dropdown" href="#"><span class="user-name"></span><span class="caret"></span></a>'
+' <ul class="dropdown-menu">'
+' <li><a href="/prj/pages/profile.html"> Profile</a></li>'
+' <li><a href="/prj/index.html"> Logout</a></li>'
+' </ul>'
+ '</li>'
+' </ul>';
navHTML =
'<nav class="navbar navbar-inverse">'
+'<div class="container-fluid">'
+'<div class="navbar-header">'
+' <button class="navbar-toggle" type="button" data-toggle="collapse"'
+' data-target="#securedNavbar">'
+' <span class="icon-bar"></span> <span class="icon-bar"></span> <span'
+' class="icon-bar"></span>'+' </button>'+'</div>'+'<div class="collapse navbar-collapse" id="securedNavbar">'
+' <ul class="nav navbar-nav">'
+' <li class="{{feeds}}"><a href="/prj/pages/feeds.html"><img src="/prj/images/darpan-logo.png" height="20px" /> Home</a></li>'
+' <li class="{{projects}}" class="dropdown"><a class="dropdown-toggle projects"'
+' data-toggle="dropdown" href="#"><span class="projects">Projects</span></a>'
+' <ul class="dropdown-menu">'
+' <li><a href="/prj/pages/project/projectList.html">List</a></li>'
+' <li><a href="/prj/pages/project/createNewProject.html">Add new project</a></li>'
+' </ul>'
+' </li>'
+' <li class="{{training}}"><a href="#">Training</a></li>'
+ appraisalMenuHTML
+' <li class="{{recruitment}}"><a href="#">Recruitment</a></li>'
+' <li class="{{users}}" class="dropdown"><a class="dropdown-toggle users"'
+' data-toggle="dropdown" href="/prj/pages/user/userList.html"><span class="users">Users</span></a>'
+' <ul class="dropdown-menu">'
+' <li><a href="/prj/pages/user/userList.html">List</a></li>'
+' <li><a href="/prj/pages/user/createNewUser.html">Add new user</a></li>'
+' </ul>'
+' </li>'
+' </ul>'
+ userOptionsHTML
+'</div>'
+'</div>'
+'</nav>';
function darpanNav(selectedTab){
alert('Object is : '+loggedInUser.toString());
theCompiledTemplate = navHTML.replace("{{"+selectedTab+"}}", "active")
newRow = $("#nav-div").html(theCompiledTemplate);
}
HTML文件是:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
.
.
.
.
.
.
<script src="../js/prj.js"></script>
<script src="/prj/js/navigation.js"></script>
<script>
darpanNav("feeds");
</script>
</body>
</html>
注意:所有必要的库和文件都已包含在内。所以不用担心。
现在,我想要实现的是基于用户角色对文件navigation.js
中的变量navHTML
的内容应用if-else条件。也就是说,如果roleId是2,那么我不想显示appraisalMenuHTML
的内容,如果是1,则显示它。
我应该如何做到这一点?
在应用条件od newHTML
时,我面临的问题是我无法访问修改后的全局变量loggedInUser
值。这是我任务中的主要障碍。
有人请帮帮我。
从我在prj.js中看到的情况来看,loggedInUser
似乎属于全局范围,所以你应该能够在navigation.js中访问它,因为它是在prj.jsp之后调用的。但请注意,在全局范围内进行任何操作都不是好的做法。
除非你有充分的理由从javascript文件创建模板(例如,如果你使用React.js),否则你应该把它放在实际的html文件中,并通过像Jade这样的模板引擎来实现逻辑。