我有兩個用戶角色限定即如何應用if-else條件向具有不同用戶角色的人顯示不同菜單?
1:角色ID爲管理員用戶和2:角色ID爲正常用戶
我試圖在兩個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;
});
}
});
第二個文件:NAVI gation.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>
注:所有必要的庫和文件已被包括在內。所以不要擔心。
現在我想根據用戶角色將if-else條件應用於來自文件navigation.js
的變量navHTML
的內容。這是如果roleId是2,那麼我不想顯示內容appraisalMenuHTML
,如果它是1顯示它。
我該怎麼做到這一點?
我在應用條件od newHTML
時遇到的問題是我無法訪問已修改的全局變量loggedInUser
值。這是我的任務中的主要障礙。
有人請幫助我。
請問您可以創建一個plunker或jsfiddle – brk