JQuery: Ajax: how can I show the boot dialog before starting and closing after closing?

I want to show the download dialog when an AJAX request is executed; and I want to use this 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;
}

//--

return - Error: there is no such "hide" method for the widget instance of the dialog

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();

}

TO RETURN:

TypeError: dlg undefined

dlg.dialog ("hide");

0
source share
3 answers

Job)))

$(".wrapper").prepend('<div id="loading" class="i hide"><img src="/theme/style/imgs/busy.gif" alt="">  </div>');

//--Add 
$("div.add_post a").click(function(){


$.ajax(
{

    url : "/add.php",
    beforeSend : function () {
        loading();

    },
    complete : function (){

    },
    success : function (data) {
        loading("stop");
        var form = $(data).find("#add_post");
        form.dialog({title : " ", modal : true});

    } 
}
);

return false;
})


//--

//--Loading dialog

function loading(type = "start")
{
    if(type == "start")
    {
        $("#loading").dialog({modal : true, minHeight: 30});
        $(".ui-dialog-titlebar").hide();
    }
    else
        $(".ui-dialog-content").dialog().dialog("close");
}

// -

0
source

Since it $.ajax()is asynchronous, you do not need to use a special function for "before sending" ... using your example, just do it before calling it $.ajax()like this:

dlg.dialog("show");
$.ajax(
    {
        url: "/add.php",
        complete: function() {
            dlg.dialog("option", "hide");
        }
    }
);

( IO? http://en.wikipedia.org/wiki/Asynchronous_I/O)

!

dlg, 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 dlg;
}

" " :

, :

var dlg //de Put this OUTSIDE $("div.add_post a").click({})

function loadingDialog(dOpts, text = " ,  ...")
{
    dlg = $("<div><img src='/theme/style/imgs/busy.gif' alt=''/> "+text+"<div>").dialog(dOpts);
    $(".ui-dialog-titlebar").hide();
}

loadingDialog({modal : true, minHeight : 80, show : true});

, , .

: http://api.jqueryui.com/dialog/#option-hide .dialog('option','hide') .dialog('hide')

+3

$("div.add_post a").click(function(){

  var dlg = loadingDialog({modal : true, minHeight : 80, show : true});
            dlg.dialog("show");
  $.ajax({
        url : "/add.php",
        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;
}
0

All Articles