"include" in javascript or something similar to reduce code size

0

I have a dashboard where when some value or information is updated, the other values are automatically recalculated without refresh.

An example is that when adding an expense in the table, it needs to recalculate all expenses, update the DB, update various other information like total expenses, balance ... Anyway I already have all this, but for ex when the person edit only the product, I need to put all that code again, because everything needs to be updated. My JS is too big because many actions are on the same page.

I searched and saw that something like PHP inlude could not be used in this case, but I would have something like putting all the information update code inside a function type

$(function() { });

This is a snippet of two distinct actions that start different but if ajax returns no errors, both need to do the same "refresh all data" function.

$(document).on('click','#addCom', function (){
var data        = $("#dataAddCom").val();
var funcionario = $("#funcionarioAddCom").val();
var valor       = $("#valorRecAddCom").val();
var idJob       = $("#idJob").html();

if(data == '' || funcionario == '' || valor == ''){
    $.notify({
        message: 'Preencha todos os campos!',
        },{
        // settings
        element: 'body',
        position: null,
        type: "danger",
        placement: {
            from: "top",
            align: "center"
        }
    });
}else{  
    $.ajax({
        type : 'POST',
        url  : '../conexao/addDespesa.php',
        data : { cadastraCom:"1", data: data, funcionario: funcionario, valor: valor, idJob: idJob },
        dataType: 'json',
        success :  function(retorno){
            if(retorno.erro == 0){

           **Varias funções que atualizam os dados**

           }else{
           }
    });
}
});

Another action (edit).

$(document).on('click','#boxCom .save-edit', function (){
var idCom       = $(this).closest('.center').siblings('.id-despesa').text();
var data        = $(this).closest('.center').siblings('.data-despesa');
var funcionario = $(this).closest('.center').siblings('.funcionario-despesa');
var valorRep    = $(this).closest('.center').siblings('.valor-rep');

var idJob      = $("#idJob").html();

var dataText = data.find('.temp input').val();
var funcionarioText = funcionario.find('.temp select').val();
var valorRepText = valorRep.find('.temp input').val();

var boxOrig = $(this).closest('.actions').find('.orig');
var boxTemp = $(this).closest('.actions').find('.temp');

if(dataText == '' || funcionarioText == '' || valorRepText == ''){
    $.notify({
        message: 'Preencha todos os campos!',
        },{
        // settings
        element: 'body',
        position: null,
        type: "danger",
        placement: {
            from: "top",
            align: "center"
        }
    });
}else{
    //alert(idCom+dataText+funcionarioText+valorRepText)
    $.ajax({
        type : 'POST',
        url  : '../conexao/addDespesa.php',
        data : { editCom:"1", dataEdit: dataText, funcionario: funcionarioText, valorRep: valorRepText, idJob: idJob, id: idCom },
        dataType: 'json',
        success :  function(retorno){
            if(retorno.erro == 0){

           **Varias funções que atualizam os dados (novamente)**

           }else{
           }
    });
}
});

Most of the code I use for the update is this way.

var valorComSoma = 0;
$('#boxCom .valor-rep').each(function (i) {   
                    valorComSoma = parseFloat($(this).text().replace(/[^0-9]/g, '')) + valorComSoma;});

$("#boxValorTotal1").text(numberParaDolar(valorDespesaSoma/100));

$(function () {
    var totalRec = 0;
    $('#boxCom tr').each(function () {
        var funcDespesa = $(this).find('.funcionario-despesa > p').text().trim();
        if (funcDespesa === 'Cesar') {
            totalRec += parseInt($(this).find('.valor-rep > p').text().replace(/[^0-9]/g, '').trim());
        }
    });
    if (resultadoFinalSoma > lucroEsperado) {
        var saldoTotal = lucroEsperado;
    } else {
        var saldoTotal = resultadoFinalSoma;
    }
    var comisao = saldoTotal * 0.45;
    var saldo = comisao - totalRec;
    $("#saldoComCesar").text(numberParaDolar(saldo / 100));
});

Would you like to put all the update code inside a function, and then just call it?

    
asked by anonymous 27.06.2017 / 23:37

1 answer

1

The code in question you can write in PHP and then include the script in the others that will use the functions.

Example: funcoes.php

<?php
var valorComSoma = 0;
$('#boxCom .valor-rep').each(function (i) {   
                    valorComSoma = parseFloat($(this).text().replace(/[^0-9]/g, '')) + valorComSoma;});

$("#boxValorTotal1").text(numberParaDolar(valorDespesaSoma/100));

$(function () {
    var totalRec = 0;
    $('#boxCom tr').each(function () {
        var funcDespesa = $(this).find('.funcionario-despesa > p').text().trim();
        if (funcDespesa === 'Cesar') {
            totalRec += parseInt($(this).find('.valor-rep > p').text().replace(/[^0-9]/g, '').trim());
        }
    });
    if (resultadoFinalSoma > lucroEsperado) {
        var saldoTotal = lucroEsperado;
    } else {
        var saldoTotal = resultadoFinalSoma;
    }
    var comisao = saldoTotal * 0.45;
    var saldo = comisao - totalRec;
    $("#saldoComCesar").text(numberParaDolar(saldo / 100));
});
?>

Then, to call the script use the following code:

<?php require_once 'funcoes.php'; ?>
    
28.06.2017 / 00:33