0

I'm using ajax and successfully getting the response. I'm unable to understand, why if-else condition is not returning boolean value as expected. Not sure, if I'm using the return in right place or not. Need help.

validateEverPresentRowWidgetValue: function (datafield, value, rowValues) {
                                if ((!value)) {
                                    $.alert({
                                        content: 'Please select Date!',
                                        btnClass: 'btn-warning',
                                        boxWidth: '30%',
                                        title: 'Missed something!',
                                        type: 'orange',
                                        typeAnimated: true,
                                        useBootstrap: false
                                    });
                                    return false; //Works fine
                                } else if (value) {
                                     $.ajax({
                                        url: 'checkForLeave.php',
                                        dataType: 'json',
                                        method: 'post',
                                        data: {date: value},
                                        success: function (response) {
                                            if (response.length > 0) {
                                                $.confirm({
                                                    title: 'Confirm!',
                                                    content: 'This date is NOT allowed! <br> Reason:' + response[0].comments,
                                                    boxWidth: '30%',
                                                    typeAnimated: true,
                                                    useBootstrap: false,
                                                    btnClass: 'btn-warning',
                                                    type: 'orange',
                                                    buttons: {
                                                        confirm: function () {
                                                            return true; //Problem in return
                                                        },
                                                        cancel: function () {
                                                            return false; //Problem in return
                                                        }
                                                    }
                                                });
                                            } else {
                                                return true; 
                                            }
                                        },
                                        error: function (response) {
                                            return false;
                                        }
                                    });
                                                                    }
                            }
  • See: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Explosion Pills Jul 24 '18 at 07:27
  • @ExplosionPills: Link provides good explanation and with my context I'm not using async:false. And to add on, code works fine for `return` statement from `If` condition. However, it's not same in `else if` with button click. There is no response from `else if()`. I guess I'm misplacing the `return` word. – user1940594 Jul 24 '18 at 11:04

0 Answers0