bart2puck
bart2puck

Reputation: 2522

using jquery dialog multiple times with slightly different parameters

I have a site in which there are alot of confirm pages and need for a dialog window. Im am wondering if there is a better way to write my code so that i dont have to spell out all of the dialog parameters every single time. Each dialog may have 1 thing different. usually the complete button is different function.

for example:

  $('#dialogA').dialog(
    {
            autoOpen:false,
            width: 800,
            modal: true,
            resizable: false,
            closeOnEscape: false,
            buttons: 
    {
            "Complete": function() 
            {
                 //DO SOMETHING FOR A(possible print results of something)
            }
    }
    });

and another

    $('#dialogB').dialog(
    {
            autoOpen:false,
            width: 800,
            modal: true,
            resizable: false,
            closeOnEscape: false,
            buttons: 
    {
            "Complete": function() 
            {
                 //DO SOMETHING FOR B (possibly some ajax call)
            }
    }
    });

so the only thing that changes is what the Complete button does. in laymen's terms i guess is I want to set a variable the contains all the dialog parameters....

Upvotes: 1

Views: 58

Answers (1)

Dirk Lachowski
Dirk Lachowski

Reputation: 3151

Extend jQuery:

(function ($) {
    $.fn.extend({
    confirmDialog: function (options) {
        var defaults = {
            autoOpen:false,
            width: 800,
            modal: true,
            resizable: false,
            closeOnEscape: false,
            buttons:
        };

        var options = $.extend(defaults, options);
        $(this).dialog(options);
        }           
    }
})(jQuery);

and call it like this:

 $('#dialogB').dialog({Complete: function() { … }; });

You can also override the defaults when call the dialog...

Upvotes: 1

Related Questions