Problem with Performance with jquery and the $ .Get method

2

Good I have this code Jquery with the method $. get the problem is that it takes me a lot of time to load it, someone gives me a help in the performans.

$(document).ready(function() {

$.get('/rolList.php', function(result) {
    var roln = new Array([]);
    roln = $.parseJSON(result);
    $(function() {
        // setup autocomplete function pulling from currencies[] array
        $('#autocomplete').autocomplete({
            lookup: roln,
            onSelect: function(suggestion) {
                var thehtml = '<strong>Currency Name:</strong> ' + suggestion.value + ' <br>';
                $('#outputcontent').html(thehtml);
            }
        });
    });
    console.log(roln);
});

// SI SOLO SE SELECCIONA EL HIJO SE AUTO CHECKEA EL PADRE
$('input[type=checkbox]').on('change', function() {
    setTimeout(
        function() {
            $("input:checkbox:checked").each(function() {
                var parents = $.trim($(this).parents('div.parent').text());
                var children = $.trim($(this).parents('div.checkbox').text());
                if(parents.length <= 0 && children.length > 0) {
                    var parentID = '#' + $(this).attr('id').substring(0, 2);
                    $(parentID).prop('checked', true);
                    $(parentID).parents('label').addClass('active');
                }
            });
        }, 100);
});
$('#autocomplete').keyup(function() {
    var val = $('#autocomplete').val();
    if(val === '') {
        $('input').hasClass('active').removeClass('active');
    }
});
//INPUT CUSTOMER CON AUTOCOMPLETE 

// SI EL ROL EXISTE SE GENERA SUS PERMISOS PARA ASI 
// MODIFICARLO


  // VALIDAR SI EL ROL EXISTE
            $('#autocomplete').on('change', function() {
             var roleName = $(this).val();
             var collaps='';
            $.get('/rolExists.php',{roleName : roleName,type:1}, function(resp) {
                var rol = $.parseJSON(resp);
                if(rol[0]._isValid == '1') {
                    $.get('/roleModulepermission.php', {
                            roleName: roleName
                        }, function(isV) {
                            var rmp = $.parseJSON(isV);
                            $.each(rmp, function(index, val) {
                                var parents = val.ParentsShortName;
                                var module = val.moduleShortName;

                                    if(parents == 'inv') {
                                        $('input[id*=' + parents + ']').prop('checked', true);
                                        $('input[id*=' + parents + ']').parents('label').addClass('active');
                                    } else {
                                        if(module != '#') {
                                            $('input[id=' + module + ']').prop('checked', true);
                                            $('input[id=' + module + ']').parents('label').addClass('active');
                                             collaps = $('input[id=' + parents + ']').parents("div[class=parent]").attr('href');
                                            $(collaps).addClass('show');
                                        }else{
                                            $('input[id=' + parents + ']').prop('checked', true);
                                            $('input[id=' + parents + ']').parents('label').addClass('active');
                                            collaps = $('input[id=' + parents + ']').parents("div[class=parent]").attr('href');
                                            $(collaps).addClass('show');
                                        }

                                    }
                                    if(roleName == 'Admin'){
                                        $('#autocomplete').prop('disabled', true);
                                        $("#addRol").prop('disabled', true);
                                    }else{
                                        $('#autocomplete').prop('disabled', true);
                                    }

                                });
                            });
                        }
                    });
            });


  // INSERTA O ACTUALIZAR ROLL
  var rolID = new Array();
   var formArray = new Array();
  $("#addRol").on("click", function () {
var rolns = $("#autocomplete").val();
// SE CREA EL ARRAY DE LOS MODULOS SELECCIONADOS
$.get("/moduleInfo.php", function (result) {
    var moduleInfo = new Array([]);
    moduleInfo = $.parseJSON(result);
    if (rolns !== '') {
        $("input:checkbox:checked").each(function () {
             var r = $.trim($(this).parents('div[class=checkbox]').text().toLowerCase());
             var parentsName = $.trim($(this).parents('div[class=parent]').text().toLowerCase());
            $.each(moduleInfo, function (i, val) {
                if (r == val.moduleName.toLowerCase() && r !== val.parents.toLowerCase()) {
                    var idModule = val.moduleID;
                    formArray.push({
                        "roleName": r,
                        "moduleID": idModule
                    });
                    console.log(idModule, r);
                }
                if (parentsName == val.parents.toLowerCase() && parentsName == val.moduleName.toLowerCase()) {
                    var IdParents = val.moduleID;
                    formArray.push({
                        "roleName": parentsName,
                        "moduleID": IdParents
                    });
                }
            });

        });
        console.log(formArray);
    } else {
        swal({
            type: "error",
            title: "ERROR!...",
            html: $("<div>")
                .addClass("swal2-small-title-error")
                .text("Defina el Nombre del Rol")
        });
    }
});
// SE VALIDA SI EL ROL ES PARA INSERT O ACTUALIZACION
$.get('/rolExists.php', {
    roleName: rolns,
    type: 1
}, function (isV) {
    var isValid = $.parseJSON(isV);
    if (isValid[0]._isValid == '0') {
        $.get('/rolExists.php', {
            roleName: rolns,
            type: 2
        }, function (insr) {
            rolID = $.parseJSON(insr);
            console.log(rolID);
            $.each(formArray, function (ind, val) {
                $.get('/insertRolePermission.php', {
                        roleID: rolID[0].roleID,
                        moduleID: val.moduleID
                    },
                    function (result) {});
            });
            location.reload();
        });

    } else {
        $.get('/rolExists.php', {
            roleName: rolns,
            type: 3
        }, function (exists) {
            rolID = $.parseJSON(exists); 
            $.get('/roleDeletePermission.php', {roleID: rolID[0].roleID}, function (e) {});
            $.each(formArray, function (ind, val) {
                $.get('/insertRolePermission.php', {
                        roleID: rolID[0].roleID,
                        moduleID: val.moduleID
                    },
                    function (result) {});
            });
            location.reload();
        });
    }
});
});
// LIMPIA LA PANTALLA DE ROL
$("#removeRol").on("click",function(){
 location.reload();
})
});
    
asked by Porfirio Dellasera 13.12.2017 в 00:17
source

1 answer

0

Holaa,

You can review the time it takes for the .php file to resolve what you are returning and if you are looking for the initial load of your development to be faster, minify the .css and .js files

Finally I leave this url of jquery tricks and good practices (using $ get takes away performance and more if you use it too many times) link

    
answered by 10.01.2018 в 06:57