ajax nette – více násobné volání ajaxu

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
ondrusu
Člen | 118
+
0
-

Ahojte, mám takový dotaz, co dělám špatně.
Mám ajaxové funkce, které si volám při rendrování presenteru nebo při kontrole formuláře v UI dialogu.
Posílám dotaz na uživatelské jméno:

                if ($('#userName').val() != '') {
                    if ($("#edit-dialog").data('action') == 'Add') {
                        $.nette.ajax({
                            type: 'GET',
                            url: "?do=existUserName",
                            data: {
                                "user": $('#userName').val()
                            },
                            success: function (data) {
                                if (data.count > 0) {
                                    $('#userName').addClass('ui-state-error');
                                    status += 'Uživatelské jméno již existuje!<br>';
                                    $('#status-error').css('display', 'block');
                                    $('#status-error').html(status);
                                    valid = false;
                                }

                            },
                            error: function (jqXHR, textStatus, errorThrown) {

                                // Ajax ERROR Dialog
                                $("<div id='infoDialog' title='Error Dialog' style='text-align: center;'>An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!<br><br>"
                                        + "<b>" + jqXHR.statusText + "</b><br>"
                                        + jqXHR.responseText + " . . . <b>More in console</b></div>").dialog({
                                    width: 480,
                                    height: 280,
                                    modal: true,
                                    draggable: false,
                                    resizable: false,
                                    buttons: {
                                        "Ok": function ()
                                        {
                                            $(this).dialog("close");
                                        }
                                    }
                                }).addClass("ui-state-error");
                            }
                        });
                    }
                    $('#userName').removeClass("ui-state-error");
                } else {
                    $('#userName').addClass('ui-state-error');
                    valid = false;
                }

V prezenteru:

    public function handleExistUserName($user) {
        if ($this->isAjax()) {
            $username = $this->database->table('users')->select('username')->where('username = ?', $user);
            $this->payload->count = count($username);
            $this->sendPayload();
        }
    }

Takhle samostatně to funguje. Když si v js za tím ajaxem vypíšu return false a do fomuláře napíšu to co už v db je vrátím mi to hlášku, která je nastavena. Ale já mám ještě za tímto javascriptem toto:

                if (valid) {
                    var uid = $("#edit-dialog").data('uid');
                    var action = $("#edit-dialog").data('action');
                    var dataObject = {
                        email: $('#email').val(),
                        firstName: $('#firstName').val(),
                        lastName: $('#lastName').val(),
                        department_id: $('#department_id').val(),
                        password: $('#pwd').val(),
                        role: $("#role").val(),
                        username: $("#userName").val(),
                        active: $("#active").val()
                    };
                    $.nette.ajax({
                        type: 'GET',
                        url: "?do=renderUser" + action,
                        data: {
                            "user_pers_id": uid,
                            "user_data": dataObject
                        },
                        success: function (data) {
                            // Add/Edit Success
                            $('.' + uid).parent().parent().find("td").each(function () {
                                $(this).animate({
                                    borderBottomColor: '#e8e8e8',
                                    borderBottomWidth: '1px',
                                    borderTopColor: '#e8e8e8',
                                    borderTopWidth: '1px'
                                }, 150)
                            });
                            $("<div id='infoDialog' title='Info' style='text-align: center;'><b>User was " + action + "</b></div>").dialog({
                                height: 80,
                                modal: true,
                                draggable: false,
                                resizable: false,
                                open: function (event, ui) {
                                    $('.ui-dialog #infoDialog').fadeOut(1200, function () {
                                        $('.ui-dialog #infoDialog').remove();
                                        // Set to defaults
                                        var numOfVisibleRows = $('tbody tr:visible').length;
                                        $('#usrcnt').text(numOfVisibleRows);
                                        $('input#search').val('');
                                        $(".dept_selector").find("option[value=0]").attr('selected', true);
                                    });
                                }
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            // Turn Off active row highlight
                            $('.' + uid).parent().parent().find("td").each(function () {
                                $(this).animate({
                                    borderBottomColor: '#e8e8e8',
                                    borderBottomWidth: '1px',
                                    borderTopColor: '#e8e8e8',
                                    borderTopWidth: '1px'
                                }, 150)
                            });
                            // Ajax ERROR Dialog
                            $("<div id='infoDialog' title='Error Dialog' style='text-align: center;'>An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!<br><br>"
                                    + "<b>" + jqXHR.statusText + "</b><br>"
                                    + jqXHR.responseText.substring(0, 360) + " . . . <b>More in console</b></div>").dialog({
                                width: 480,
                                height: 280,
                                modal: true,
                                draggable: false,
                                resizable: false,
                                buttons: {
                                    "Ok": function ()
                                    {
                                        $(this).dialog("close");
                                    }
                                }
                            }).addClass("ui-state-error");


                            $('#result').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
                            console.log('jqXHR:');
                            console.log(jqXHR);
                            console.log('textStatus:');
                            console.log(textStatus);
                            console.log('errorThrown:');
                            console.log(errorThrown);
                        }
                    }
                    );
                    //$(this).dialog("close");
                }
                else {
                    $('#status-error').css('display', 'block');
                    $('#status-error').html(status);
                }
            },
            Cancel: function () {
                var uid = $("#edit-dialog").data('uid');
                $('.' + uid).parent().parent().find("td").each(function () {
                    $(this).animate({
                        borderBottomColor: '#e8e8e8',
                        borderBottomWidth: '1px',
                        borderTopColor: '#e8e8e8',
                        borderTopWidth: '1px'
                    }, 150)
                });
                $(this).dialog("close");
            }
        }
    });

No a když smažu return false (psal jsem víše), tak console píše
localhost/www/setup/users?do=existUserName&user=admin
Provisional headers are shown
No a vůbec nevim kde je chyba. Nesetkali jste se s ím někdo?
Díky

David Matějka
Moderator | 6445
+
0
-

V nette.ajax.js je unique extension, ktere budes muset vypnout, aby ti to fungovalo, vice na https://github.com/…ette.ajax.js#…

ondrusu
Člen | 118
+
0
-

díky vyzkouším a dám vědět

ondrusu
Člen | 118
+
0
-

toto by mělo být ono že …
$.nette.ext('unique', null);