I want to show a loading dialog while an AJAX request is in process; and I want to use that piece of code anywhere in my project.
//--Add
$("div.add_post a").click(function(){
var dlg = loadingDialog({modal : true, minHeight : 80, show : true});
$.ajax(
{
url : "/add.php",
beforeSend : function (){
dlg.dialog("show");
},
complete : function (){
dlg.dialog("hide");
}
}
);
return false;
})
//--
//--Loading dialog
function loadingDialog(dOpts, text = "пожалуйста подождите, идет загрузка...")
{
var dlg = $("<div><img src='/theme/style/imgs/busy.gif' alt='загрузка'/> "+text+"<div>").dialog(dOpts);
$(".ui-dialog-titlebar").hide();
return dialog;
}
//--
NEW CODE
//--Add
$("div.add_post a").click(function(){
var dlg = loadingDialog();
$.ajax(
{
url : "/add.php",
complete : function (){
dlg.dialog("hide");
},
success : function (data) {
$(data).find("#add_post").dialog();
}
}
);
return false;
})
//--
//--Loading dialog
function loadingDialog(dOpts = {modal : true, minHeight : 80, show : true}, text = "пожалуйста подождите, идет загрузка...")
{
var dlg = $("<div><img src='/theme/style/imgs/busy.gif' alt='загрузка'/> "+text+"<div>").dialog(dOpts);
$(".ui-dialog-titlebar").hide();
return dlg;
}
//--
returned - Error: no such method 'hide' for dialog widget instance
LAST NEW CODE
//--Add
$("div.add_post a").click(function(){
var dlg;
loadingDialog();
$.ajax(
{
url : "/add.php",
complete : function (){
dlg.dialog("hide");
},
success : function (data) {
var form = $(data).find("#add_post");
form.dialog({title : "Добавить признание", modal : true});
}
}
);
return false;
})
//--
//--Loading dialog
function loadingDialog(dOpts = {modal : true, minHeight : 80, show : true}, text = "пожалуйста подождите, идет загрузка...")
{
dlg = $("<div><img src='/theme/style/imgs/busy.gif' alt='загрузка'/> "+text+"<div>").dialog(dOpts);
$(".ui-dialog-titlebar").hide();
}
RETURNED :
TypeError: dlg is undefined
dlg.dialog("hide");
Working )))
//--
Since
$.ajax()
is asynchronous, you don't need to use a special function for 'before send' ... using your example, just do it before calling$.ajax()
like this:( What is asynchronous IO? http://en.wikipedia.org/wiki/Asynchronous_I/O )
Note!
In your example you create and store the dialog in
dlg
, but then you returndialog
which is one problem. Using your code, it ought to be this:After your 'new code' I add this advice:
Create the dialog outside the function, in the global namespace, like this:
Then return nothing, just setup your loading dialog with the function which modifies a global variable outside the namespace of the function.
Note: http://api.jqueryui.com/dialog/#option-hide It is
.dialog('option','hide')
not.dialog('hide')
I would try