Question

Am jConfirm for user confirmation.

My first jConfirm doesnt stop for user action and just passes to next.

My Code:

    $(function () {

    $("#UpdateJobHandler").click(function () {

        var JobHander = getJobHandler();
        if (JobHander.MaxInstances == 0) {
                jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                    if (!ans)
                        return;
                });
        }

        var json = $.toJSON(JobHander);

        $.ajax({
            url: '../Metadata/JobHandlerUpdate',
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

                var message = data.Message;
                var alertM = data.MessageType;
                if (alertM == 'Error') {
                    $("#resultMessage").html(message);

                }

                if (alertM == 'Success') {
                    $("#resultMessage").empty();
                    alert(alertM + '-' + message);
                    action = "JobHandler";
                    controller = "MetaData";
                    loc = "../" + controller + "/" + action;
                    window.location = loc;
                }

                if (alertM == "Instances") {
                    jConfirm(message, 'Instances Confirmation?', function (answer) {
                        if (!answer)
                            return;
                        else {
                            var JobHandlerNew = getJobHandler();
                            JobHandlerNew.FinalUpdate = "Yes";
                            var json = $.toJSON(JobHandlerNew);
                            $.ajax({

                                url: '../Metadata/JobHandlerUpdate',
                                type: 'POST',
                                dataType: 'json',
                                data: json,
                                contentType: 'application/json; charset=utf-8',
                                success: function (data) {

                                    var message = data.Message;
                                    $("#resultMessage").empty();
                                    alert(alertM + '-' + message);
                                    action = "JobHandler";
                                    controller = "MetaData";
                                    loc = "../" + controller + "/" + action;
                                    window.location = loc;
                                }
                            });
                        }
                    });
                }
            }
        });
    });
});

What am i missing?

Was it helpful?

Solution

Not sure if this is all, but this part:

    if (JobHander.MaxInstances == 0) {
            jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                if (!ans)
                    return;
            });
    }

probably doesn't do what you want. It is exiting the function(ans) { ... } function, while you probably want to exit the whole handler, i.e. $("#UpdateJobHandler").click(function () { ... }. If so, you would need to do similar to what you do below - i.e. put the whole thing in function(ans) { ... }, after the return. Probably best to separate into smaller functions.

EDIT: Something along these lines:

    function afterContinue() {
       var json = $.toJSON(JobHander);

       $.ajax({
          // ... all other lines here ...
       });
    }

    if (JobHander.MaxInstances == 0) {
            jConfirm('Continue?', 'Current Maximum Instances', function (ans) {
                if (ans) {
                   afterContinue();
                }
            });
    }

You can do similar thing for all the success functions.

Another example, you can rewrite the Instances check like this:

            function afterInstances() {
                        var JobHandlerNew = getJobHandler();
                        JobHandlerNew.FinalUpdate = "Yes";

                        // ... and everything under else branch ...
            }

            if (alertM == "Instances") {
                jConfirm(message, 'Instances Confirmation?', function (answer) {
                    if (answer) {
                       afterInstances();
                    }
                });
            }

Important - rename the methods (afterContinue, afterInstances, ...) to have some name that means something useful to someone reading this in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top