Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Serving two different functions withing the same js file in an MVC app (no **Jquery** solution please)

I am building an MVC app which will have two different plans for users, one of them with restrictions on some functionalities provided via js. What I need is to serve both plans within the same app but I am a little confused in how would be the best way to implement it. I have planned to include both codes within the same js file and comment both, the restricting function and the un-restricting one and then try to create a function which allows the users to access their plan on a fixed condition. I am wondering if it is even possible to be done and if it is, then someone to bring me a simple example which I could be able to re-factor and reproduce within my app environment.

Sorry if I do not provide any code but it would be very complicated to display here the code of my app. I think I would be done with just a simple example like this:

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.

If someone is able to bring such an example, or any link to a similar question already answered (which I could not be able to fine out there) it will be highly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Let's say you were doing this on a PHP page:

<?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>

So the whole point is, to access your user information, make the plan available somewhere in the HTML. Then read the plan from javascript and behave differently depending on it. Also, no need to maintain multiple functions, just use the same and put the condition of the plan inside.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda