How to create functions with callback?

4

Can anyone tell me how to create functions with callback? I have created this function but it is not working.

        function PostsCategoriasListar(table, select, callback) {
        if (table) {
            $.ajax({
                url: controller_dashboard,
                type: 'POST',
                dataType: 'json',
                data: {acao: 'PostsCategoriasListar', Listar: 'table'},
                success: function(response) {
                    if (response.status === 'LogOff') {
                        window.setTimeout(function() {
                            window.location.reload();
                        }, 10000);
                    }
                    $element.find('table tbody').html(response.table);
                    ExecuteDataTable(0, maximo, 1);
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }
        if (select) {
            $.ajax({
                url: controller_dashboard,
                type: 'POST',
                dataType: 'json',
                data: {acao: 'PostsCategoriasListar', Listar: 'select'},
                success: function(response) {
                    if (response.status === 'LogOff') {
                        window.setTimeout(function() {
                            window.location.reload();
                        }, 10000);
                    }
                    $element.find('form select').html(response.select);
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }
        //EU QUERO QUE O CALLBACK SO EXECULTE DEPOIS QUE TODO CODIGO QUE ESTIVER NOS IF'S ACIMA TENHAN TERMINADO, ELE SEMPRE EXECULTA ANTES ONDE ESTOU ERRANDO?
        if (typeof callback === 'function')
            callback();
    }

PS: I did not put the ajax requests because it would make it very large, because they are working only if I need them to execute first and after the callback is executed

    
asked by anonymous 22.08.2014 / 01:54

1 answer

3

You need to call callback when you receive the ajax request response. Note that in your logic it may be that two ajax requests are made ( if (table) and if (select) ). The code below assumes that is not the case , so I coke the if select in the else of the first if.

function PostsCategoriasListar(table, select, callback) {
    if (table) {
        $.ajax({
            url: controller_dashboard,
            type: 'POST',
            dataType: 'json',
            data: {acao: 'PostsCategoriasListar', Listar: 'table'},
            success: function(response) {
                if (response.status === 'LogOff') {
                    window.setTimeout(function() {
                        window.location.reload();
                    }, 10000);
                } else {
                    $element.find('table tbody').html(response.table);
                    ExecuteDataTable(0, maximo, 1);
                    callback();
                }
            },
            error: function(error) {
                console.log(error);
                callback(error);
            }
        });
    } else if (select) {
        $.ajax({
            url: controller_dashboard,
            type: 'POST',
            dataType: 'json',
            data: {acao: 'PostsCategoriasListar', Listar: 'select'},
            success: function(response) {
                if (response.status === 'LogOff') {
                    window.setTimeout(function() {
                        window.location.reload();
                    }, 10000);
                } else {
                    $element.find('form select').html(response.select);
                    callback();
                }
            },
            error: function(error) {
                console.log(error);
                callback(error);
            }
        });
    } else {
        callback('Ou table ou select precisam ter um valor');
    }
}

One more suggestion: call the same callback in case of error (passing error as parameter). In this case, who called the PostsCategoriasListar function can know if an error occurred or not:

PostsCategoriasListar(function(error) {
    if (!error) {
         // executado com sucesso
    } else {
        // houve um erro na chamada
    }
});
    
22.08.2014 / 02:03