Nathan A
Nathan A

Reputation: 11319

Global Events With Unobtrusive Ajax

When Unobtrusive ajax performs an ajax call, it doesn't appear to call the global jquery ajax events. Here is an example of my attempt to handle the event.

$(document).ajaxComplete(function () { alert('test'); });

The event works fine if I make ajax calls like this:

$.ajax({
    url: "/myurl",
    type: 'GET',
    success: function (data) { //do something }
});

Any idea how to set up a global handle for all ajax complete events, regardless of their source? Or how to handle global events for Unobtrusive Ajax?

Upvotes: 2

Views: 866

Answers (1)

BlackjacketMack
BlackjacketMack

Reputation: 5692

We were running into the same problem. For some reason .ajaxComplete was not working with unobtrusive ajax. However, we did find that .ajaxStart and .ajaxStop were always being called.

Here's how we're using it to handle a spinner and re-parse the validation on dynamic content:

$(document).ajaxStart(function (event, jqxhr, settings) {
        $("#ajax-spinner").show();
    });

    $(document).ajaxStop(function (event, jqxhr, settings) {
        $("#ajax-spinner").hide();

        $("form").each(function () {
            var form = $(this);
            form.removeData('validator');
            form.removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse(form);
        });
    });

Upvotes: 1

Related Questions