Automatic OnClick in function

0

Personal explaining a little how the system works.

When the user enters a value below the established goal, it automatically opens a modal to fill in an "action plan".

When you complete the plan, it can create other actions or complete, but if it leaves open, every time when entering this place, another modal will appear, stating that there is an open plan.

If the user closes the system and returns to analyze this plan, the certain seriousness: This "Modal Info" appears clicking on the button and it would take you to the plan page.

Err: On return I do not know what the hell I did, the OnClick was automatic. As soon as it enters the area, the system detects that it has an open plan, and it already opens its page. Here is the code for the function below.

function showModals(indicadorID, name, city, month, year, role){
    checkPlan(name, city, year, function (result) {
        //Exemplo de operacao
        //mes a definir em indicadorForaDaMeta
        checkIndicadorMeta(name, city, month, year, role, function (indicadorForaDaMeta) {
            var mesAtual = new Date().getMonth();
            if (result.length == 0) {
                var mesDoUltimoPlano = mesAtual - 1;
            } else {
                var mesDoUltimoPlano = new Date(result[result.length - 1].pla_data).getMonth();
            }
            console.log(result);
            if (indicadorForaDaMeta && !(mesAtual == mesDoUltimoPlano)) {
                $('#add-modal-plano-de-acao-form').find('[name="id"]').val(indicadorID);
                $('#add-modal-plano-de-acao').modal('show');
            } else {
                for (let i = 0; i < result.length; i++) {
                    if (result[i].pla_status) {
                        var month = new Date(result[i].pla_data).getMonth() + 1;
                        var year = new Date(result[i].pla_data).getFullYear();
                        $('#info-modal-plano-de-acao-form').find('[name="id"]').val(result[i].pla_id);
                        $('#info-modal-plano-de-acao-form').find('[name="month"]').val(month);
                        $('#info-modal-plano-de-acao-form').find('[name="year"]').val(year);
                        $('#info-modal-plano-de-acao').modal('show');
                        console.log("#" + convertNameToId(name) + "-botao-plano");
                        $('#' + convertNameToId(name) + '-botao-plano').removeAttr('onClick');
                        $('#' + convertNameToId(name) + '-botao-plano').attr('onClick', abrirPlano(result[i].pla_id, month, year));
                       
                        i = result.length; 
                    }
                }
            }
        });

    });
}
    
asked by anonymous 23.05.2018 / 14:13

1 answer

0

This happens because in this line:

$('#' + convertNameToId(name) + '-botao-plano').attr('onClick', abrirPlano(result[i].pla_id, month, year));

You are expressly saying that the attribute "onclick" has the value of the result of the function.

Try switching to use a callback :

$('#' + convertNameToId(name) + '-botao-plano').attr('onClick', function() { abrirPlano(result[i].pla_id, month, year) });

So, the onclick attribute gets the value of the anonymous function which when called will call the function abrirPlano(....)

    
23.05.2018 / 14:47