在 MVC 应用程序的同一个 js 文件中提供两个不同的功能(请不要使用 **Jquery** 解决方案)
Serving two different functions withing the same js file in an MVC app (no **Jquery** solution please)
我正在构建一个 MVC 应用程序,它将有两个不同的计划
用户,其中之一对所提供的某些功能有限制
通过 js。我需要的是在同一个应用程序中同时提供这两个计划,但我
对如何实现它的最佳方式有点困惑。我有
计划将这两个代码包含在同一个 js 文件中并对两者进行注释,
限制功能和非限制功能,然后尝试
创建一个功能,允许用户访问他们的计划
固定条件。我想知道它是否均匀
有可能完成,如果可以,那么有人会给我一个简单的
我可以在我的应用程序中重构和重现的示例
环境。
抱歉,如果我不提供任何代码,但这样做会非常复杂
在这里显示我的应用程序的代码。我想我会做一个
像这样的简单示例:
user A is restricted and user B is unrestricted. The main.js
file will allow the users to add text lines to the index.html
file. The restriction would be to limit the amount of lines that
could be added by the user A, let's say to 5 lines, while the
user B would be able to add as many lines as he wants. To do
that, as I imagined and said before, both, the addLine function
and
the restrictedAddLine function
would be commented. Then, if user
A access the app the main.js file will be served with the
restrictedAddLine function
uncommented and the addLine function
still commented, and viceversa if it is the user B the
one who access the app.
如果有人能够举出这样的例子,或者任何 link 类似的例子
问题已经回答(我无法在那里解决)
将不胜感激。
假设您在 PHP 页面上执行此操作:
<?php
// this is a string containing the plan of current logged user
$user_plan=$database->getCurrentUser()->plan;
?>
<html>
<head>
<script text/javascript>
// set a global js variable with the plan
var userPlan='<?php echo $user_plan; ?>';
</script>
</head>
<body>
<input type="button" value="Add line" onclick="add_line()" />
<script text/javascript>
function add_line() {
if (userPlan=='basic') {
alert('You can only add one line');
return;
} else {
// add as many lines
}
}
</script>
</body>
</html>
所以重点是,要访问您的用户信息,请在 HTML 中的某处提供计划。然后阅读 javascript 中的计划并根据它采取不同的行为。还有,不用维护多个函数,用一样的,把plan的条件放在里面就行了。
我正在构建一个 MVC 应用程序,它将有两个不同的计划 用户,其中之一对所提供的某些功能有限制 通过 js。我需要的是在同一个应用程序中同时提供这两个计划,但我 对如何实现它的最佳方式有点困惑。我有 计划将这两个代码包含在同一个 js 文件中并对两者进行注释, 限制功能和非限制功能,然后尝试 创建一个功能,允许用户访问他们的计划 固定条件。我想知道它是否均匀 有可能完成,如果可以,那么有人会给我一个简单的 我可以在我的应用程序中重构和重现的示例 环境。
抱歉,如果我不提供任何代码,但这样做会非常复杂 在这里显示我的应用程序的代码。我想我会做一个 像这样的简单示例:
user A is restricted and user B is unrestricted. The main.js file will allow the users to add text lines to the index.html file. The restriction would be to limit the amount of lines that could be added by the user A, let's say to 5 lines, while the user B would be able to add as many lines as he wants. To do that, as I imagined and said before, both, the
addLine function
and therestrictedAddLine function
would be commented. Then, if user A access the app the main.js file will be served with therestrictedAddLine function
uncommented and theaddLine function
still commented, and viceversa if it is the user B the one who access the app.
如果有人能够举出这样的例子,或者任何 link 类似的例子 问题已经回答(我无法在那里解决) 将不胜感激。
假设您在 PHP 页面上执行此操作:
<?php
// this is a string containing the plan of current logged user
$user_plan=$database->getCurrentUser()->plan;
?>
<html>
<head>
<script text/javascript>
// set a global js variable with the plan
var userPlan='<?php echo $user_plan; ?>';
</script>
</head>
<body>
<input type="button" value="Add line" onclick="add_line()" />
<script text/javascript>
function add_line() {
if (userPlan=='basic') {
alert('You can only add one line');
return;
} else {
// add as many lines
}
}
</script>
</body>
</html>
所以重点是,要访问您的用户信息,请在 HTML 中的某处提供计划。然后阅读 javascript 中的计划并根据它采取不同的行为。还有,不用维护多个函数,用一样的,把plan的条件放在里面就行了。