ajax loadend,jquery - Modal Forms - AjaxStart and End - .load and .ajax() calls - Stack Overflow

I am working on an .NET MVC 3 project where I have made all the form based views load in a modal dialog using jQuery.

All of my views are loaded using ajax throughout the project.

I have a jQuery function I call for any link click event which fetches the given url (of a form based view like edit, add, etc) and displays it in a modal dialog as follows:

function getFormInModal(url_location, modalWidth, modaltitle, tab) {

var url = url_location;

var dialogDiv = $('

dialogDiv.load(url, function () {

//Enable the client side validation

var $form = $('form', dialogDiv);

$.validator.unobtrusive.parse($form);

var buttons = {

"Submit": function () {

//Create a validation summary container

var valSummary = ModalForms.validationSummary();

valSummary.setup($form);

if ($form.valid()) {

var postUrl = $form.attr('action');

var formData = $form.serialize();

jQuery.ajax({

url: postUrl,

type: 'POST',

data: formData,

complete: function (data) {

dialogDiv.dialog('close');

refresh(tab);

$.jGrowl("Submitted Successfully.", { life: 2000 });

},

error: function (jqXHR, textStatus, errorThrown) {

var data = jQuery.parseJSON(jqXHR.responseText);

if (data && data.errors) {

valSummary.addErrors(data.errors);

}

}

});

}

},

Cancel: function () {

dialogDiv.dialog('close');

$("html,body").css("overflow", "auto");

}

};

//Load up the dialog

dialogDiv.dialog({

autoOpen: true,

modal: true,

title: modaltitle,

draggable: false,

resizable: false,

buttons: buttons,

width: modalWidth,

close: function () {

$(this).remove();

},

open: function (event, ui) {

$("html,body").css("overflow", "hidden");

}

});

});

}

My problem is in the success function of the submit button of the modal.

It does 3 things:

1. Closes the modal form dialog

2. Notifies the user that the POST was successful

3. Refreshes the content in a particular DIV via a call to another simple function refresh().

function refresh(tabname) {

if (tabname == "dashboard") {

$("#dashboard-tab").load("/Sales/Home/Reports");

$("#tabs").tabs("select", 0);

}

if (tabname == "leads") {

$("#leads-tab").load("/Sales/Lead/Index");

$("#tabs").tabs("select", 1);

}

if (tabname == "retail") {

$("#retail-tab").load("/Sales/Retail/Index");

$("#tabs").tabs("select", 2);

}

if (tabname == "corporate") {

$("#corporate-tab").load("/Sales/Corporate/Index");

$("#tabs").tabs("select", 3);

}

}

Now, separately I have hooked up some events on DOM ready which handle my ajax call status as follows:

$("#loading").dialog({

autoOpen: false,

modal: true,

width: '450px',

title: 'Patience is a virtue!',

resizable: false,

draggable: false

});

$("#loading").ajaxStart(function () {

$("#loading").dialog('open');

$("html,body").css("overflow", "hidden");

});

$("#loading").ajaxSuccess(function () {

$("#loading").dialog('close');

$("html,body").css("overflow", "auto");

});

$("#loading").ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {

$("#loading").dialog('close');

$.jGrowl(html(thrownError.toString()), { life: 2000 });

});

Everything works perfectly -

- I click on a link

- I see the #loading modal dialog

- It loads and the #loading closes and the dialogDiv modal opens

- I click submit and the #loading opens up on top of the dialogDiv

- When it is done both close and i get the jGrowl notification

The final step which is the loading of the content again also happens, however once my POST is done, and the .load() call is made to refresh content the #loading modal does not show again.

Basically, .ajaxStart() is not firing. I do not know why.

First I had written an success function for the ajax post made in the submit button's click event. Then I realized that complete might be better.

Even now, thou I am calling the refresh() and hence the .load() after the completion of the only other live .ajax() call, the .ajaxStart() is not working.

Can someone please HELP!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值