1

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....

1
  • Have you tried using the Complete: function(param) with this and storing self? And a parser (i.e., if or switch) inside the Complete function to determine how it should behave? Commented Jan 8, 2014 at 17:47

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.