How to call the recursive function by passing this

Asked

Viewed 69 times

0

I am having problems locking the browser in executing the code below. The problem occurs when I call the function: montarMenu.call(this);, recursively, inside itself.

var SISTEMA= SISTEMA|| {};
SISTEMA.MenuPrincipal = (function() {

    function MenuPrincipal() {  
        this.sidebar = $("#sidebarnav");
        this.listaMenuJSON = [];
        this.strHTML = "";
        this.indice = 0;        
    }

    MenuPrincipal.prototype.iniciar = function() {      
        getJSON.call(this);
    }   

    function getJSON() {
        var resposta = $.ajax({
            url: '/menus',
            method: 'GET',
            contentType: 'application/json'  
        });
        resposta.done(onMontarMenuCarregado.bind(this));    
    }

    function onMontarMenuCarregado(JSON) {
        this.listaMenuJSON = JSON;

        while (this.indice < this.listaMenuJSON.length) {           
            if (this.listaMenuJSON[this.indice].menuPai == null) {              
                montarMenu.call(this);              
            }           
            this.indice++; 
        }                   

        this.sidebar.html(this.strHTML);        
    }       

    function montarMenu() {                     
        var itemMenu = this.listaMenuJSON[this.indice];             
        var numeroColunas = ((itemMenu.numeroColunas == null || !itemMenu.numeroColunas.trim()) ? "" : " class='"+itemMenu.numeroColunas+"'");

        this.strHTML = "<li"+numeroColunas+">";     
        if (itemMenu.menuPai == null) {         
            this.strHTML+="<a class='has-arrow' href='"+((itemMenu.url == null || !itemMenu.url.trim()) ? '#' : itemMenu.url)+"' aria-expanded='false'><i class='mdi "+itemMenu.icone+"'></i>";
            this.strHTML+="<span class='hide-menu'>"+itemMenu.nome+" </span>";
        } else {
            this.strHTML+="<a class='has-arrow' href='#' aria-expanded='false'>";
            this.strHTML+=itemMenu.nome;
        }           
        this.strHTML+="</a>";
        this.strHTML+="<ul aria-expanded='false' class='collapse'>";

        this.indice++;
        while (this.indice < this.listaMenuJSON.length) {
            if (this.listaMenuJSON[this.indice].menuPai == null) {
                break;
            }                       

            if (itemMenu.id == this.listaMenuJSON[this.indice].menuPai.id  && this.listaMenuJSON[this.indice].possuiSubmenu == false) {                 
                this.strHTML+="<li><a href='"+((this.listaMenuJSON[this.indice].url == null || !this.listaMenuJSON[this.indice].url.trim()) ? '#' : this.listaMenuJSON[this.indice].url)+"'>"+this.listaMenuJSON[this.indice].nome+"</a></li>";
            } else {                    
                if (this.listaMenuJSON[this.indice].possuiSubmenu == true) {
                    montarMenu.call(this);
                }
            }   
        }       

        this.strHTML+="</ul>";
        this.strHTML+="</li>";
    }

    return MenuPrincipal;

}());

$(function() {

    var menuPrincipal = new SISTEMA.MenuPrincipal();
    menuPrincipal.iniciar();

});
  • Tried using . bind(this)?

  • César Felipe, sorry for the delay, but also hangs.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.