Why is this Ajax conditional dialog not working?

128 views Asked by At

Why is this code not working?

In the meantime (trying to make it work) i have changed it a dosen time, but i can't find the solution.

Anyone has an idea? I have no errors in console.

First of all, it check's if a dialog needs to be opened.

This is the workflow:

If DialogRequired => Dialog.Click = OK --> execute an ajax call If DialogRequired => Dialog.Click = Cancel --> do nothing If Dialog NOT Required => execute an ajax call

$(function () {
    $("a.orderlink").unbind();

    $("a.orderlink").bind("click", function () {
        var ProductID = $(this).parent().attr("data-productid");
        var Ammount = $(this).parent().parent().find("input#ammount").val();

        $.ajax({ type: "post",
            url: $(this).attr("href").replace("AddToCart", "ExistsInCart"),
            data: { ProductId: $(this).parent().attr("data-productid") },
            succes: function (data) {
                if (data == 1) {
                    $("#ProductExistsInOrder").dialog({
                        autoOpen: true,
                        height: 170,
                        width: 400,
                        modal: true,
                        buttons: {
                            "OK": function () {
                                /*acties om toe te voegen $.ajax()*/
                                $.ajax({ type: "post",
                                    url: $(this).attr("href"),
                                    data: { ProductId: ProductID, Ammount: Ammount },
                                    succes: function () {
                                        $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken");
                                    }
                                });
                                setTimeout("location.reload(true);", 100);
                                $(this).dialog("close");
                                location.reload(true);
                            //    return false;
                            },
                            "Annuleer": function () {
                                $(this).dialog("close");
                             //   return false;
                            }
                        }
                    });
                } else {
                    $.ajax({ type: "post",
                        url: $(this).attr("href"),
                        data: { ProductId: ProductID, Ammount: Ammount },
                        succes: function () {
                            $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken");
                        }
                    });


                };
                // $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken");
            },
            error: function (XMLHeeptRequest, textStatus, errorThrown) {
                alert(textStatus);
                alert(errorThrown);
            }
        });
        // alert("end");
        //  AddToCart(this);
        return false;
    });
   // return false;
});
// ProductId: $(orderlinkObject).parent().attr("data-productid"), Ammount: $(orderlinkObject).parent().parent().find("input#ammount").val()   

This is how it goes:

  • Gets called (=ok) : /Cart/ExistsInCart with parameter: product ID and returns true in jSon
  • But the dialog isn't called and i can't seem to update it with firebug.
1

There are 1 answers

1
PetersenDidIt On

Looks like you are having problems with your scope.

You have alot of $(this).attr("href") in anonymous functions in ajax success. In those function this != "a.orderlink".

You will want to do var that = $(this) up at the top of your click handler and then use that.attr("href").

Example: http://jsbin.com/ivoniv/edit#javascript